2013-11-14 87 views
0

我对这个问题很头疼,我不知道是否有任何1可以帮助我。在我的测试和BCC中,我总能正确看到PDF附件,但也许有10%的人将PDF文件视为已损坏(我知道有些人使用Outlook,我正在使用Mac邮件)。PHP邮件PDF附件获取文件损坏

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: [email protected]".$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;} 

可能发生了什么?我总是看到它的工作,但很少有人无法打开文件..

+0

使用[swiftmailer.org(http://swiftmailer.org/)和你的生活会更容易;) –

+0

我建议你[PHPMailer的( https://github.com/Synchro/PHPMailer)。 – Nelson

+0

,我可以从html2pdf输出附上pdf吗? –

回答

0

已经有一段时间了,但终于解决了这个问题。问题在于PHP_EOL,在我的情况下,它返回\ n,而有些系统的电子邮件应该有\ r \ n作为换行符。 要解决此问题,只需将新的$ EOL:

$eol = "\r\n"; 
0

你设置标题的方式似乎对我来说。然而,两件事情我注意到/有什么不同:

$headers .= "Content-Type: multipart/mixed;{$eol}\tboundary=\"".$separator."\"".$eol; 

借此* /从最终

$body .= $message.$eol;*/ 

和内容处置掉:

"Content-Disposition: attachment; filename=\"" . $filename . "\"".$eol; 

而且,身体和附件标题应与标题组合在一起,不需要在邮件中单独发送邮件():

return mail($mailto, $subject, "", $headers);