简体   繁体   English

邮件附件pdf文件

[英]Mail attachment pdf file

I'm using html2pdf to generate a pdf file, and I will send it by mail attachement. 我正在使用html2pdf生成pdf文件,并将通过邮件附件发送。

1- I've created a pdf file: works fine 1-我已经创建了一个pdf文件:可以正常工作

2- I've saved it: works fine. 2-我保存了:工作正常。

3- I've attached it for send mail but it doesn't work: I've received a mail with pdf attached but impossible to open it! 3-我已将其附加以发送邮件,但无法正常工作:我已收到一封附有pdf的邮件,但无法打开它! (filesize < normal filesize). (文件大小<常规文件大小)。 And when I resend a mail again, works fine ! 当我再次发送邮件时,效果很好!

Do you have any suggestions please? 您有什么建议吗?

My php code: 我的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 );

(Tested and attachment received on a SINGLE run) (测试和对接收的附件SINGLE运行)

You had some single quotes which should have been double quotes and did not close the text body with a semi-colon. 您有一些单引号应该是双引号,并且没有用分号关闭文本主体。

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

Has been changed to: (with closing semi-colon) 已更改为:(以分号结尾)

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

If what I posted below doesn't work, try replacing this line: 如果我在下面发布的内容无效,请尝试替换此行:

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

with: 与:

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

which is what I used to test it with, since I do not have the same setup as you do, using an $id variable. 这就是我用来测试它的方法,因为我没有使用与您相同的设置,而是使用$id变量。

Rewrite: (Successfully tested with one of my PDF files) 重写:(已成功通过我的一个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