Send multi-part encoded mail with attachments.


/ Published in: PHP
Save to your folder(s)

Sends mail to the e-mail address specified. Supports attaching files, multi-part message, and e-mail encodings. Works well with Asian cell phones.


Copy this code and paste it in your HTML
  1. /**
  2.  * Sends mail to the e-mail address given.
  3.  * Supports attaching files and multiple encodings.
  4.  * @param string The email address of the recipient
  5.  * @param string The name to include in the from header
  6.  * @param string The e-mail address to include in the from header
  7.  * @param string the e-mail subject
  8.  * @param string the e-mail body.
  9.  * @param boolean true if the body is html or false if plain text.
  10.  * @param string a path to a file on the server to attach to the e-mail. Null or an empty string indicates that there is no attachment.
  11.  * @param string the encoding with which to encode the subject, from, and body.
  12.  */
  13. function SendMail($emailaddress,
  14. $from, $fromaddress,
  15. $emailsubject="",
  16. $body="", $html = true,
  17. $attachment="",
  18. $encoding="utf-8") {//{{{
  19.  
  20. # Is the OS Windows or Mac or Linux
  21. if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
  22. $eol="
  23. ";
  24. } elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
  25. $eol="\r";
  26. } else {
  27. $eol="\n";
  28. }
  29.  
  30. //set subject encoding
  31. if (!empty($emailsubject)) {
  32. $emailsubject = encode_mail_string($emailsubject, $encoding);
  33. }
  34. $from = encode_mail_string($from, $encoding);
  35. if ($encoding != "utf-8" && !empty($body)) {
  36. $body = mb_convert_encoding($body, $encoding, "utf-8");
  37. }
  38.  
  39. $msg = "";
  40.  
  41. # Common Headers
  42. $headers .= "From: ".$from." <".$fromaddress.">".$eol;
  43. $headers .= "Reply-To: ".$from." <".$fromaddress.">".$eol;
  44. $headers .= "Return-Path: ".$from." <".$fromaddress.">".$eol; // these two to set reply address
  45. $headers .= "Message-ID: <".time()." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
  46. $headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
  47. $headers .= 'MIME-Version: 1.0'.$eol;
  48.  
  49. if (!empty($attachment)) {
  50. //send multipart message
  51. # Boundry for marking the split & Multitype Headers
  52. $mime_boundary=md5(time());
  53. $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
  54.  
  55. # File for Attachment
  56.  
  57. $f_name = $attachment;
  58. $handle=fopen($f_name, 'rb');
  59. $f_contents=fread($handle, filesize($f_name));
  60. $f_contents=chunk_split(base64_encode($f_contents));//Encode The Data For Transition using base64_encode();
  61. $f_type=filetype($f_name);
  62. fclose($handle);
  63.  
  64. # Attachment
  65. $msg .= "--".$mime_boundary.$eol;
  66. $msg .= "Content-Type: application/jpeg; name=\"".$file."\"".$eol;
  67. $msg .= "Content-Transfer-Encoding: base64".$eol;
  68. $msg .= "Content-Disposition: attachment; filename=\"".basename($attachment)."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
  69. $msg .= $f_contents.$eol.$eol;
  70. # Setup for text OR html
  71. $msg .= "Content-Type: multipart/alternative".$eol;
  72.  
  73.  
  74. $contentType = "text/plain";
  75. if ($html) {
  76. $contentType = "text/html";
  77. }
  78.  
  79. # Body
  80. $msg .= "--".$mime_boundary.$eol;
  81. $msg .= "Content-Type: ".$contentType."; charset=\"".$encoding."\"".$eol;
  82. $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
  83. $msg .= $body.$eol.$eol;
  84.  
  85. # Finished
  86. $msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.
  87. } else {
  88. $headers .= "Content-Type: text/plain; charset=\"".$encoding."\"".$eol;
  89. $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
  90. $msg .= $body.$eol.$eol;
  91. }
  92.  
  93. // SEND THE EMAIL
  94. //LogMessage("Sending mail to: ".$emailaddress." => ".$emailsubject);
  95.  
  96. //ini_set(sendmail_from, '[email protected]'); // the INI lines are to force the From Address to be used !
  97. ini_set(sendmail_from, $fromaddress); //needed to hopefully get by spam filters.
  98. $success = mail($emailaddress, $emailsubject, $msg, $headers);
  99. ini_restore(sendmail_from);
  100.  
  101. return $success;
  102. }//}}}

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.