php发送电子邮件附件
我似乎无法找到这个PHP函数的问题,我写的应该发送附件的电子邮件。我一直在努力挣扎。php发送电子邮件附件
function myMail($to, $subject, $mail_msg, $filename, $contentType){
$random_hash = md5(date('r', time()));
$headers = "From: [email protected]\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";
}
编辑的问题是,在邮件的消息与文件混合并作为附件发送。
Artefacto让我看看输出与更多的关注,我发现修复:
function myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){ $random_hash = md5(date('r', time())); $headers = "From: [email protected]\r\nReply-To: ".$to; $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; $attachment = chunk_split(base64_encode(file_get_contents($pathToFilename))); 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: $contentType; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash-- "; $message = ob_get_clean(); $fh=fopen('log.txt','w'); fwrite($fh,$message); $mail_sent = @mail($to, $subject, $message, $headers); return $mail_sent ? "Mail sent" : "Mail failed"; }
一年后非常有用:-) – Neal 2011-04-13 18:35:48
除非您这样做了解MIME邮件的内部运行情况,否则标准答案是使用邮件库库,如PHPMailer或Swiftmailer,它们可以处理开箱即用的附件。
SwiftMailer示例如何附加文件是here。
我刚刚看了几封电子邮件,我注意到最终附件边界以' - '结尾,而开始边界标记没有。在你的代码,您有:
--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--
看一看这里的例子:
+1,用于清理手头脚本中的问题。 – 2010-06-22 12:56:46
@Pekka谢谢,但也许我的努力是徒劳的。尽管得到了正确的解决方案,但看起来同样的解决方案是独立完成的:-( – Mike 2010-06-22 13:06:35
这些是我使用的标题,他们一直像魅力一样工作。
$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";
你不是说什么行不通。 – 2010-06-22 12:02:33
包含整个输出,包含头文件(减去base64编码文件) – Artefacto 2010-06-22 12:16:17