簡體   English   中英

郵件附件pdf文件

[英]Mail attachment pdf file

我正在使用html2pdf生成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 );

(測試和對接收的附件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 );
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM