Posted By

stancox on 11/02/09


Tagged

email


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

satsuma
cesgra
Priestd09


Send Hassle Free and Dependable HTML Emails With PHP


 / Published in: PHP
 

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

  1. function htmlmail($to, $subject, $message, $headers = NULL)
  2. {
  3. $mime_boundary = md5(time());
  4.  
  5. $headers .= "\nMessage-ID: <" . time() . " TheSystem@{$_SERVER['SERVER_NAME']}>\n";
  6. $headers .= "X-Mailer: PHP " . phpversion() . "\n";
  7. $headers .= "MIME-Version: 1.0\n";
  8. $headers .= "Content-Type: multipart/alternative;boundary={$mime_boundary}\n\n";
  9.  
  10. $newmessage = "This is a multi-part message in MIME format.";
  11. $newmessage .= "\n\n--{$mime_boundary}\n";
  12. $newmessage .= "Content-type: text/plain;charset=utf-8\n\n";
  13. $newmessage .= strip_tags(str_replace(array('<br>', '<br />'), "\n", $message)) . "\n\n";
  14.  
  15. $newmessage .= "\n\n--{$mime_boundary}\n";
  16. $newmessage .= "Content-type: text/html;charset=utf-8\n\n";
  17.  
  18. // prepended HTML
  19. $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">';
  20.  
  21. // HTML message that was passed to this function
  22. $newmessage .= $message;
  23.  
  24. // appended HTML
  25. $newmessage .= '</td></tr></table></td></tr></table></body>';
  26.  
  27. return mail($to, $subject, $newmessage, $headers);
  28. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: blockhead123 on December 1, 2010

i get garbage mime text all over when i receive the mail. why is that?

Posted By: blockhead123 on December 2, 2010

ok i resolved it. the garbage came from this statement -> $headers .= "\nMessage-ID: \n";

from how i see it. we need to remove that statement. one more thing the function is not dynamic. cause if you need an html based message you will need to have a separate plain message at the same time. the parameter for message is wrong, it should have two paramters for message, htmlmessage and plainmessage for it to have a custom look for each type.

You need to login to post a comment.