繁体   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