2013-03-27 63 views
1

PDF文件正在由tcpdf庫生成,但我不能附加到電子郵件。它發送一個空的1kb PDF文件的電子郵件。電子郵件TCPDF生成的PDF文件與PHP郵件功能

$to = '[email protected]'; 
$subject = 'Receipt'; 
$repEmail = '[email protected]'; 

$fileName = 'receipt.pdf'; 
$fileatt = $pdf->Output($fileName, 'S'); 
$attachment = chunk_split($fileatt); 
$eol = PHP_EOL; 
$separator = md5(time()); 

$headers = 'From: Sender <'.$repEmail.'>'.$eol; 
$headers .= 'MIME-Version: 1.0' .$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 

$message = "--".$separator.$eol; 
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$message .= "This is a MIME encoded message.".$eol; 

$message .= "--".$separator.$eol; 
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 

$message .= "--".$separator.$eol; 
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol; 
$message .= "Content-Disposition: attachment".$eol.$eol; 
$message .= $attachment.$eol; 
$message .= "--".$separator."--"; 

if (mail($to, $subject, $message, $headers)){ 
echo "Email sent"; 
} 

else { 
echo "Email failed"; 
} 

我知道它的多用PHPMailer的容易,但我需要用郵件功能來做到這一點,不使用任何圖書館。 任何幫助表示讚賞。

+0

它不應該是$附件= chunk_split($文件名); – vodich 2013-03-27 15:36:52

+0

你在開玩笑嗎? $ filename是用於附件的11位數字的「名稱」 – 2013-03-27 15:45:29

+0

請在使用phpMailer等優秀庫的時候請不要使用PHP的內置'mail()'函數發送附件,這會讓您的生活變得更加輕鬆。儘管你在問題中提到了什麼,但真的沒有理由不使用它。 – Spudley 2013-03-27 15:45:54

回答

0

解決方案是在vodich發送鏈接。

您的代碼是正確的。你必須改變一個行:

$fileatt = $pdf->Output($fileName, 'S'); 

$fileatt = $pdf->Output($fileName, 'E'); 
+0

它沒有工作! – 2013-03-31 17:17:24

+0

這很奇怪。我已經複製了您的代碼,只更改了這一行並在我的本地機器上進行了測試,並且可以正常工作我已收到完整內容的電子郵件。我正在使用Ubuntu 12.04,爲本地測試安裝sendmail ... – developer 2013-03-31 19:39:00

相關問題