PHP发送附件的电子邮件
我似乎无法find这个PHP函数的问题,我写的应该发送附件的电子邮件。 我一直在苦苦挣扎。
function myMail($to, $subject, $mail_msg, $filename, $contentType){ $random_hash = md5(date('r', time())); $headers = "From: webmaster@example.com\r\nReply-To: ".$to; $headers .= "\r\nContent-Type: ".$contentType. "; boundary=\"PHP-mixed-".$random_hash."\""; $attachment = chunk_split(base64_encode(file_get_contents($filename))); ob_start(); echo " --PHP-mixed-$random_hash Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\" --PHP-alt-$random_hash Content-Type: text/plain; charset=\"utf-8\" Content-Transfer-Encoding: 7bit $mail_msg --PHP-alt-$random_hash --PHP-mixed-$random_hash-- Content-Type: text/plain; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash-- "; $message = ob_get_clean(); $mail_sent = @mail( $to, $subject, $message, $headers ); return $mail_sent ? "Mail sent" : "Mail failed"; }
编辑问题是邮件的消息与文件混合并作为附件发送。
我刚才看了几封电子邮件,注意到最后的附件边界以“ – ”结尾,而开始的边界标记却没有。 在你的代码中,你有:
--PHP-mixed-$random_hash-- Content-Type: text/plain; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash--
也许应该是:
--PHP-mixed-$random_hash Content-Type: text/plain; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash--
看看这里的例子:
Artefacto让我看着输出更多的关注,我发现修复:
函数myMail($ to,$ subject,$ mail_msg,$ filename,$ contentType,$ pathToFilename){ $ random_hash = md5(date('r',time())); $ headers =“From:webmaster@mysite.com \ r \ n回复至:”。$ to; $ headers。=“\ r \ nContent-Type:multipart / mixed; boundary = \”PHP-mixed - “。$ random_hash。”\“”; $ attachment = chunk_split(base64_encode(file_get_contents($ pathToFilename))); ob_start(); 回声“ --PHP-混色$ random_hash 内容types:multipart / alternative; 边界= \ “PHP-ALT-$ random_hash \” --PHP-ALT-$ random_hash 内容types:text / plain; 字符集= \ “UTF-8 \” 内容传输编码:7位 $ mail_msg --PHP-ALT-$ random_hash-- --PHP-混色$ random_hash 内容types:$ contentType; 名字= \ “$文件名\” 内容传输编码:base64 内容处置:附件 $附件 --PHP-混色$ random_hash-- “; $ message = ob_get_clean(); $ FH =的fopen( 'log.txt的', 'W'); 的fwrite($跳频,$消息); $ mail_sent = @mail($ to,$ subject,$ message,$ headers); 返回$ mail_sent? “邮件发送”:“邮件失败”; }
除非你这样做了解MIME邮件的内部工作,否则标准答案是使用像PHPMailer或Swiftmailer这样的邮件程序库来处理附件。
SwiftMailer关于如何附加文件的例子在这里 。
这些是我使用的标题,他们一直像魅力。
$base = basename($_FILES['upload']['name']); $file = fopen($randname_path,'rb'); $size = filesize($randname_path); $data = fread($file,$size); fclose($file); $data = chunk_split(base64_encode($data)); //boundary $div = "==Multipart_Boundary_x".md5(time())."x"; //headers $head = "From: $from\n". "MIME-Version: 1.0\n". "Content-Type: multipart/mixed;\n". " boundary=\"$div\""; //message $mess = "--$div\n". "Content-Type: text/plain; charset=\"iso-8859-1\"\n". "Content-Transfer-Encoding: 7bit\n\n". "$message\n\n". "--$div\n". "Content-Type: application/octet-stream; name=\"$base\"\n". "Content-Description: $base\n". "Content-Disposition: attachment;\n". " filename=\"$base\"; size=$size;\n". "Content-Transfer-Encoding: base64\n\n". "$data\n\n". "--$div\n"; $return = "-f$from";