Posted By

iTony on 09/01/09


Tagged

email php html


Versions (?)

Who likes this?

11 people have marked this snippet as a favorite

DrewDouglass
wizard04
umang_nine
iroybot
jmiller
tapdrup
nb109
mmcachran
satsuma
qubestream
tux-world


Sending HTML emails in PHP


 / Published in: PHP
 

URL: http://dev-tips.com/featured/send-hassle-free-and-dependable-html-emails-with-php

  1. /*
  2. EXAMPLE: htmlmail('[email protected]', 'Look ma, HTML e-mails','You just got <a href="http://www.yougotrickrolled.com/">Rick Rolled</a>');
  3. NOTE: $headers is optional, but can be used to set From, CC, etc. Go to http://www.htmlite.com/php029.php for info
  4. */
  5.  
  6. function htmlmail($to, $subject, $message, $headers = NULL)
  7. {
  8. $mime_boundary = md5(time());
  9.  
  10. $headers .= "\nMessage-ID: <" . time() . " TheSystem@{$_SERVER['SERVER_NAME']}>\n";
  11. $headers .= "X-Mailer: PHP " . phpversion() . "\n";
  12. $headers .= "MIME-Version: 1.0\n";
  13. $headers .= "Content-Type: multipart/alternative;boundary={$mime_boundary}\n\n";
  14.  
  15. $newmessage = "This is a multi-part message in MIME format.";
  16. $newmessage .= "\n\n--{$mime_boundary}\n";
  17. $newmessage .= "Content-type: text/plain;charset=utf-8\n\n";
  18. $newmessage .= strip_tags(str_replace(array('<br>', '<br />'), "\n", $message)) . "\n\n";
  19.  
  20. $newmessage .= "\n\n--{$mime_boundary}\n";
  21. $newmessage .= "Content-type: text/html;charset=utf-8\n\n";
  22.  
  23. // prepended HTML
  24. $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">';
  25.  
  26. // HTML message that was passed to this function
  27. $newmessage .= $message;
  28.  
  29. // appended HTML
  30. $newmessage .= '</td></tr></table></td></tr></table></body>';
  31.  
  32. return mail($to, $subject, $newmessage, $headers);
  33. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: DrewDouglass on September 2, 2009

Thank you for giving credit where it is due, I really appreciate that. Hope you found it useful :-)

You need to login to post a comment.