Send Hassle Free and Dependable HTML Emails With PHP

Sunday, January 18th, 2009

No matter what people are telling you, I'll be the first to say it. Plain text e-mails are ugly. Most of your customers/readers/whatever can see HTML messages. So why aren't you sending them? Sexy & colorful HTML e-mails pop off the screen. Plain text e-mails make me fall asleep.

Outside of being sexier, they help with conversion. If you want to send an e-mail with a link back to your Website, how will you accomplish that with plain text? Please don't expect everyone to copy + paste. That's so last year. You're probably not sending HTML e-mails because, until today, the code has been presented in long, drawn out explanations that you have to piece together. Ouch.

So here I am to answer your dreams: A simple straightforward function that sends HTML e-mails with a plain text counterpart for those over 80 and still using AOL 2.5.

Our Script

/*
EXAMPLE: htmlmail('user@domain.com', 'Look ma, HTML e-mails','You just got <a href="http://www.yougotrickrolled.com/">Rick Rolled</a>');
NOTE: $headers is optional, but can be used to set From, CC, etc. Go to http://www.htmlite.com/php029.php for info
*/

function htmlmail($to, $subject, $message, $headers = NULL)
{
	$mime_boundary = md5(time()); 

	$headers .= "\nMessage-ID: <" . time() . " TheSystem@{$_SERVER['SERVER_NAME']}>\n";
	$headers .= "X-Mailer: PHP " . phpversion() . "\n";
	$headers .= "MIME-Version: 1.0\n";
	$headers .= "Content-Type: multipart/alternative;boundary={$mime_boundary}\n\n";

	$newmessage = "This is a multi-part message in MIME format.";
	$newmessage .= "\n\n--{$mime_boundary}\n";
	$newmessage .= "Content-type: text/plain;charset=utf-8\n\n";
	$newmessage .= strip_tags(str_replace(array('<br>', '<br />'), "\n", $message)) . "\n\n";

	$newmessage .= "\n\n--{$mime_boundary}\n";
	$newmessage .= "Content-type: text/html;charset=utf-8\n\n";

	// prepended HTML
	$newmessage .= '<body style="margin:0"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td bgcolor="#ffffff" valign="top"><table width="750" border="0" cellpadding="0" cellspacing="0" align="center"><tr><td bgcolor="#ffffff" width="750">';

	// HTML message that was passed to this function
	$newmessage .= $message;

	// appended HTML
	$newmessage .= '</td></tr></table></td></tr></table></body>';

	return mail($to, $subject, $newmessage, $headers);
}

There it is! Just copy and paste that bad boy wherever you want and begin using it today. With the prepended and appended HTML I have already in the function, your e-mails will be in a 750 pixel wide container that's centered.

Don't get carried away with your HTML+CSS wizardry, though! The mantra of HTML e-mails is, go back to the basics and don't leave it up to the e-mail client. Here are a few tips to get you rollin' in the right direction:

  1. Forget everything you know about semantics, XHTML, etc. When it comes to HTML e-mails, this represents best practice: <table width="750" height="100" border="0" cellpadding="0" cellspacing="0" align="left">. Most e-mail programs won't understand your sophisticated XHTML mumbo jumbo. Use HTML tables to position everything.
  2. Use inline CSS. That's right. Use of the <style type="text/css"> will make your HTML e-mails look fugly in some web-based e-mail clients. Use something like <b style="font-size:12px; color:#f00">.
  3. Stick to basic HTML tags and style with inline CSS. Sort of an extension of #2. Instead of <h1> , use <b> and style with CSS to your liking. Don't leave it up to e-mail clients to decide what an H1 should look like! Instead of <ul>, use &nbsp;&nbsp;&nbsp;&bull;&nbsp;.
  4. Put the width and height on everything you can. This includes images, HTML table cells, etc. Don't leave it up to e-mail clients to lay stuff out for you.

If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Brian Cray is a marketing & Internet strategy consultant, web developer, and Apple fanatic living in Columbus, Ohio, USA. His work has been featured by Mashable, NETTUTS, Smashing Magazine, and many others. He's happy to share his knowledge with you on DevTips and his own blog at Brian Cray.com.
  1. John B.
    January 18, 2009 at 6:25 pm
  2. January 18, 2009 at 7:08 pm
    • January 18, 2009 at 11:26 pm
      • January 19, 2009 at 6:24 am
  3. January 19, 2009 at 11:47 am
  4. January 19, 2009 at 12:01 pm
  5. January 21, 2009 at 10:45 am
  6. January 21, 2009 at 11:30 am
    • January 21, 2009 at 1:07 pm
  7. January 22, 2009 at 4:54 am
  8. January 22, 2009 at 3:07 pm
  9. Andrew
    March 2, 2009 at 4:05 am
  10. hamlet
    March 3, 2009 at 12:01 am
  11. March 4, 2009 at 12:58 pm
  12. March 4, 2009 at 1:18 pm
  13. Mattie
    March 4, 2009 at 3:58 pm
    • March 20, 2009 at 7:46 pm
  14. Marcus Uy
    April 17, 2009 at 8:06 am
  15. June 22, 2009 at 3:52 am
  16. July 3, 2009 at 8:44 am
  17. dan
    September 15, 2009 at 7:14 am
  18. dan
    September 15, 2009 at 8:28 am
  19. Tom
    October 14, 2009 at 5:20 am
  20. October 14, 2009 at 12:59 pm
  21. Harold
    October 30, 2009 at 7:57 am
    • October 30, 2009 at 12:24 pm
  22. warren
    October 30, 2009 at 5:36 pm
  23. December 1, 2009 at 6:36 pm

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates