2013-11-14 85 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);