首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP邮件PDF附件文件损坏

PHP邮件PDF附件文件损坏
EN

Stack Overflow用户
提问于 2013-11-14 14:17:40
回答 2查看 4.8K关注 0票数 1

我对这个问题非常头疼,我想知道any1是否能帮我解决这个问题。在我的测试和密件抄送中,我总是正确地看到PDF附件,但可能有10%的人认为PDF文件已损坏(有些人我知道他们在使用Outlook,我使用的是来自Mac的邮件)。

代码语言:javascript
复制
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;}

可能会发生什么事?我总是看到它起作用,但很少有人不能打开这个文件。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-08 15:29:49

已经有一段时间了,但终于解决了这个问题。问题是在PHP_EOL上,在我的例子中,它返回\n,而一些系统电子邮件应该有\r\n作为中断行。要解决此问题,只需放置新的$eol:

代码语言:javascript
复制
$eol = "\r\n";
票数 2
EN

Stack Overflow用户

发布于 2013-11-14 15:21:51

你设置标题的方式对我来说是正确的。然而,我注意到/做了一些不同的事情:

代码语言:javascript
复制
$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"".$eol;

把这个从结尾拿开

代码语言:javascript
复制
$body .= $message.$eol;*/

关于内容处置:

代码语言:javascript
复制
"Content-Disposition: attachment; filename=\"" . $filename . "\"".$eol;

此外,主体和附件标题应该合并到标头中,不需要在mail()中单独发送主体:

代码语言:javascript
复制
return mail($mailto, $subject, "", $headers);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19980033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档