发送包含附件的多部分编码邮件。
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.
/** * Sends mail to the e-mail address given. * Supports attaching files and multiple encodings. * @param string The email address of the recipient * @param string The name to include in the from header * @param string The e-mail address to include in the from header * @param string the e-mail subject * @param string the e-mail body. * @param boolean true if the body is html or false if plain text. * @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. * @param string the encoding with which to encode the subject, from, and body. */ function SendMail($emailaddress, $from, $fromaddress, $emailsubject="", $body="", $html = true, $attachment="", $encoding="utf-8") {//{{{ # Is the OS Windows or Mac or Linux $eol=" "; $eol=" "; } else { $eol=" "; } //set subject encoding $emailsubject = encode_mail_string($emailsubject, $encoding); } $from = encode_mail_string($from, $encoding); } $msg = ""; # Common Headers $headers .= "From: ".$from." <".$fromaddress.">".$eol; $headers .= "Reply-To: ".$from." <".$fromaddress.">".$eol; $headers .= "Return-Path: ".$from." <".$fromaddress.">".$eol; // these two to set reply address $headers .= 'MIME-Version: 1.0'.$eol; //send multipart message # Boundry for marking the split & Multitype Headers $headers .= "Content-Type: multipart/related; boundary="".$mime_boundary.""".$eol; # File for Attachment $f_name = $attachment; $f_contents=chunk_split(base64_encode($f_contents));//Encode The Data For Transition using base64_encode(); # Attachment $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: application/jpeg; name="".$file.""".$eol; $msg .= "Content-Transfer-Encoding: base64".$eol; $msg .= "Content-Disposition: attachment; filename="".basename($attachment).""".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $f_contents.$eol.$eol; # Setup for text OR html $msg .= "Content-Type: multipart/alternative".$eol; $contentType = "text/plain"; if ($html) { $contentType = "text/html"; } # Body $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: ".$contentType."; charset="".$encoding.""".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $body.$eol.$eol; # Finished $msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection. } else { $headers .= "Content-Type: text/plain; charset="".$encoding.""".$eol; $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $body.$eol.$eol; } // SEND THE EMAIL //LogMessage("Sending mail to: ".$emailaddress." => ".$emailsubject); //ini_set(sendmail_from, '[email protected]'); // the INI lines are to force the From Address to be used ! return $success; }//}}}
以上是关于发送包含附件的多部分编码邮件。的主要内容,如果未能解决你的问题,请参考以下文章
使用 perl 发送带有文件附件的多部分文本/html 替代消息,
JavaMail 从字符串发送邮件附件 - 编码 UTF-8
Python:多部分 html 电子邮件通过嵌入式图像和附件发送