我对这个问题非常头疼,我想知道any1是否能帮我解决这个问题。在我的测试和密件抄送中,我总是正确地看到PDF附件,但可能有10%的人认为PDF文件已损坏(有些人我知道他们在使用Outlook,我使用的是来自Mac的邮件)。
function mail_attachment($content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "Invitation.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $content;
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: Myself <".$from_mail.">\nBCC: me@hotmail.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message;
$body .= $eol.$eol;
// message
$body .= "Content-Type: text/plain; charset=\"utf-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;*/
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator.$eol;
// send message
$em = mail($mailto, $subject, $body, $headers);
return $em;}可能会发生什么事?我总是看到它起作用,但很少有人不能打开这个文件。
发布于 2014-01-08 15:29:49
已经有一段时间了,但终于解决了这个问题。问题是在PHP_EOL上,在我的例子中,它返回\n,而一些系统电子邮件应该有\r\n作为中断行。要解决此问题,只需放置新的$eol:
$eol = "\r\n";发布于 2013-11-14 15:21:51
你设置标题的方式对我来说是正确的。然而,我注意到/做了一些不同的事情:
$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"".$eol;把这个从结尾拿开
$body .= $message.$eol;*/关于内容处置:
"Content-Disposition: attachment; filename=\"" . $filename . "\"".$eol;此外,主体和附件标题应该合并到标头中,不需要在mail()中单独发送主体:
return mail($mailto, $subject, "", $headers);https://stackoverflow.com/questions/19980033
复制相似问题