发送包含附件的多部分编码邮件。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发送包含附件的多部分编码邮件。相关的知识,希望对你有一定的参考价值。

Sends mail to the e-mail address specified. Supports attaching files, multi-part message, and e-mail encodings. Works well with Asian cell phones.
  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=" ";
  26. } else {
  27. $eol=" ";
  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()." [email protected]".$_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. }//}}}

以上是关于发送包含附件的多部分编码邮件。的主要内容,如果未能解决你的问题,请参考以下文章

使用 perl 发送带有文件附件的多部分文本/html 替代消息,

JavaMail 从字符串发送邮件附件 - 编码 UTF-8

Python:多部分 html 电子邮件通过嵌入式图像和附件发送

发送包含嵌入图像的多部分 html 电子邮件

使用 mailkit 发送的多部分电子邮件的文本/纯文本版本未正确接收

生成带有 Excel XLSX 附件的电子邮件