PHP mail() with HTML output in a Simple to re-use function

///PHP mail() with HTML output in a Simple to re-use function

PHP mail() with HTML output in a Simple to re-use function

Ok, I could go into a long description here but I won’t I know most of you who are going to end up landing on this page are only here for one thing, and one thing only. Code. So Ill keep it short.. I know generally speaking searching for this concept is not the easiest task. I did a couple years ago cause I got sick of doing one of two things. First was just send plain text email to people through my sites. Or second manually open an email and format my email to look pretty that way. Both of which I didn’t like to do. Seeing as your here that tells me you have a similar logic to some extent at least. So with that I present an easy to re-use function that will conform PHP’s core mail() function into something you can use to send email that is formatted with HTML. Without having to go use some chunky over weight plugin or 3rd party service that will cost you an arm and a leg monthly to use.\r\n\r\nThe basis is. You build a string that is your formatted HTML for the body and template it accordingly with variables to use along side with your forms. So it can auto insert the name, email, whatever else have you into the message being sent. Then the mail gets sent. The function is also deviced to mimic the behavior of PHP’s mail() where its the same syntax as the function to call it.\r\n\r\nBare in mind this post is basically your gate way to the concept. Of which I may dive deeper into in another post one day. I may refine this function a bit in the future as I do have a couple ideas floating around recently to enhance it based on a need someone recently brought to my attention but again that’s another post for another day.\r\n\r\nNow on to the code.\r\n
\r\n\r\n		\r\n		\r\n		'.$sysSubj.'\r\n		\r\n		\r\n		\r\n		\r\n		
'.$sysMsg.'
\r\n \r\n \r\n ';\r\n $headers = 'MIME-Version: 1.0' . "\r\n";\r\n $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";\r\n $headers .= 'Content-type: text/html; charset=us-ascii' . "\r\n";\r\n $headers .= 'From:' .$fromWho. "\r\n";\r\n $headers .= 'Reply-To: ' .$fromWho. "\r\n";\r\n $headers .= '1\r\nX-MSMail-Priority: High' . "\r\n";\r\n $headers .= 'X-Mailer: MonkeyTooth Productions Mail Controller v2.0' . "\r\n";\r\n mail($toWho, $sysSubj, $sysMsg, $headers);\r\n }\r\n?>\r\n
\r\n\r\nWorth mentioning is $sysMsg This would be your HTML string essentially anything you would want between the body tags on a site would go in this string. However if you notice the whole $msgTemplate in the function you will notice it looks exactly like the typical source of a HTML page by itself, and it is. With that you can also apply style tags to the head like you would to a site.\r\n\r\nOnly thing to remember is if your including images whether it be through CSS styling or in the body. You have to give them the full URL from your site as any attached images in an email will not work, not to mention it makes the email bulkier in download size, and as a general rule of thumb although possible its not worth the mess you don’t typically attach files to emails coming from web based apps. You link to them from your server. So what I am generally saying with this is if your going to send nice looking emails through your own personalized template make an extra directory on your server and put the images or what ever have you in there so they will always be accessable to the web so when someone opens up the email there email isp/software can download the images in the viewer.\r\n\r\nNote: Depending on how you form your email they can end up in spam boxes, it sucks I know, but theres no one way to prevent it from happening on all systems around the world each company has different filters and it just happens. If your email is ending up in spam boxes globally, maybe its time to reconsider what your saying and or doing with them. Its also worth checking to see if your HOST has been blacklisted as a lot of Shared hosting providers are.
By |2014-01-05T10:29:32+00:00January 27th, 2012|Code, PHP|4 Comments

About the Author:

Not much to know about me, I'm a 35+ year old coder, geek, gamer..

4 Comments

  1. Jacob September 13, 2012 at 11:58 pm

    dogonthe leash

  2. Jacob September 13, 2012 at 11:59 pm

    kjhllkjh

  3. eysey February 12, 2013 at 10:50 am

    I’ve try this code but the email received is plaintext and show all html code

    • chris February 12, 2013 at 4:48 pm

      This unfortunately could be due to a number of reasons. Anything from internal server settings on your end. To the client settings of the method you choose to view your emails. That said other reasons could be but not limited to, changing the headers so they don’t match your domain, your domain could be blacklisted or the hosting providers ip..also could be the mail server attached to your hosting account. I’ve used this method for some years now with little to no problem from it. so with so many different X factors it’s hard to assess without knowing more.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: