2014-01-21 151 views
0

我使用html2pdf生成PDF文件,我會通過郵件附件發送它。郵件附件pdf文件

1 - 我已經創建了一個PDF文件:正常工作

2 - 我已經保存它:正常工作。

3我附上它發送郵件,但它不起作用:我收到一封附帶pdf的郵件,但無法打開它! (文件大小<正常文件大小)。當我再次發送郵件時,工作正常!

你有什麼建議嗎?

我的PHP代碼:

$filename='facture.pdf'; 
$mail_to = $email; 
$subject = "Facture"; 
$random_hash = md5(time()); 
$headers = "From:" .$mailnotif." \r\nReply-To: mondmain.fr"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
$path = 'http://mondmain.fr/Factures/'.$id.'/'.$filename.''; 
$attachment = @chunk_split(base64_encode(file_get_contents($path))); 
$message = "--PHP-mixed-$random_hash\r\n" 
."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n"; 
$message .= "--PHP-alt-$random_hash\r\n" 
."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" 
."Content-Transfer-Encoding: 7bit\r\n\r\n"; 

//Insert the plain text message. 
$message .= strip_tags($subject); 
$message .= "\r\n\r\n--PHP-alt-$random_hash\r\n" 
."Content-Type: text/html; charset=\"utf-8\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n"; 

//Insert the html message. 
$message .= 'Bonjour, 
Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain.fr.' 
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n"; 

//include attachment 
$message .= "--PHP-mixed-$random_hash\r\n" 
."Content-Type: application/doc; name=\"$filename\"\r\n" 
."Content-Type: application/pdf; name=\"$filename\"\r\n" 
."Content-Type: application/docx; name=\"$filename\"\r\n" 
."Content-Transfer-Encoding: base64\r\n" 
."Content-Disposition: attachment\r\n\r\n"; 

$message .= $attachment; 
$message .= "/r/n--PHP-mixed-$random_hash--"; 

//send the email 
mail($mail_to, $subject , $message, $headers); 
+0

這將有很大的幫助,看你使用的代碼。 – mbarthelemy

+0

我的代碼php是以上,我不能將它添加在評論(長文本:) :) – Nashen

+0

在同一頁面php page1.php,我有一個鏈接到page2.php(我可以生成和保存pdf)和在page2.php我有一個行動返回page1.php和發送郵件... – Nashen

回答

0

(經過測試,在SINGLE運行收到附件)

你有一些單引號這應該是雙引號,並沒有關閉文本體一個分號。

$message .= 'Bonjour, Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain.fr.' 

已更改爲:(與關閉分號)

$message .= "Bonjour, Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain.fr"; 

如果我下面貼不行,請嘗試更換這行:

$attachment = @chunk_split(base64_encode(file_get_contents($path))); 

$attachment = @chunk_split(base64_encode(file_get_contents($filename))); 

這是我用過的用來測試它,因爲我沒有和你一樣的設置,所以使用了一個$id變量。

重寫:(用我的PDF文件的一個試驗成功)

<?php 

$filename='facture.pdf'; 
$mail_to = $email; 
$subject = "Facture"; 
$random_hash = md5(time()); 
$headers = "From:" .$mailnotif." \r\nReply-To: mondmain.fr"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

$path = 'http://mondmain.fr/Factures/'.$id.'/'.$filename.''; 

$attachment = @chunk_split(base64_encode(file_get_contents($path))); 
$message = "--PHP-mixed-$random_hash\r\n" ."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n"; 
$message .= "--PHP-alt-$random_hash\r\n" ."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n"; 

//Insert the plain text message. 
$message .= strip_tags($subject); 
$message .= "\r\n\r\n--PHP-alt-$random_hash\r\n" 
."Content-Type: text/html; charset=\"utf-8\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n"; 

//Insert the html message. 
$message .= "Bonjour, Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain"; 
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n"; 

//include attachment 
$message .= "--PHP-mixed-$random_hash\r\n" 
."Content-Type: application/doc; name=\"$filename\"\r\n" 
."Content-Type: application/pdf; name=\"$filename\"\r\n" 
."Content-Type: application/docx; name=\"$filename\"\r\n" 
."Content-Transfer-Encoding: base64\r\n" 
."Content-Disposition: attachment\r\n\r\n"; 

$message .= $attachment; 
$message .= "/r/n--PHP-mixed-$random_hash--"; 

//send the email 
mail($mail_to, $subject , $message, $headers); 
?> 
+0

是的,但不是工作,filesize pdf attach = 50k!無法打開文件 – Nashen

+0

'50k'是什麼意思? 50兆字節? @Nashen –

+0

如果這就是您的意思是50k(50 MB),那麼您的服務器可能會限制您的文件大小,並可能與上傳最大設置有關。閱讀關於文件附件的內容:http://en.wikipedia.org/wiki/Email_attachment @Nashen ---你可以通讀這篇文章,其中有人有類似的問題http://www.roundcubeforum.net/index.php ?主題= 2398。0 –