邮件附件PDF文件

  • 本文关键字:文件 PDF php email pdf
  • 更新时间 :
  • 英文 :


我正在使用html2pdf生成pdf文件,我将通过邮件附件发送。

1-我已经创建了一个pdf文件:工作正常

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

3-我已将其附加到发送邮件,但它不起作用:我收到了带有PDF的邮件,但无法打开它!(Filesize< normal Filesize)。当我再次重新发送邮件时,工作正常!

您有什么建议吗?

我的php代码:

$filename='facture.pdf';
$mail_to = $email;
$subject = "Facture";
$random_hash = md5(time());
$headers = "From:" .$mailnotif." rnReply-To: mondmain.fr";
$headers .= "rnContent-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_hashrn"
."Content-Type: multipart/alternative; boundary="PHP-alt-$random_hash"rnrn";
$message .= "--PHP-alt-$random_hashrn"
."Content-Type: text/plain; charset="iso-8859-1"rn"
."Content-Transfer-Encoding: 7bitrnrn";
//Insert the plain text message.
$message .= strip_tags($subject);
$message .= "rnrn--PHP-alt-$random_hashrn"
."Content-Type: text/html; charset="utf-8"rn" ."Content-Transfer-Encoding: 7bitrnrn";
//Insert the html message.
$message .= 'Bonjour,
Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain.fr.'
$message .="rnrn--PHP-alt-$random_hash--rnrn";
//include attachment
$message .= "--PHP-mixed-$random_hashrn"
."Content-Type: application/doc; name="$filename"rn"
."Content-Type: application/pdf; name="$filename"rn"
."Content-Type: application/docx; name="$filename"rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachmentrnrn";
$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)));

with:

$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." rnReply-To: mondmain.fr";
$headers .= "rnContent-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_hashrn" ."Content-Type: multipart/alternative; boundary="PHP-alt-$random_hash"rnrn";
$message .= "--PHP-alt-$random_hashrn" ."Content-Type: text/plain; charset="iso-8859-1"rn" ."Content-Transfer-Encoding: 7bitrnrn";
//Insert the plain text message.
$message .= strip_tags($subject);
$message .= "rnrn--PHP-alt-$random_hashrn"
."Content-Type: text/html; charset="utf-8"rn" ."Content-Transfer-Encoding: 7bitrnrn";
//Insert the html message.
$message .= "Bonjour, Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain";
$message .="rnrn--PHP-alt-$random_hash--rnrn";
//include attachment
$message .= "--PHP-mixed-$random_hashrn"
."Content-Type: application/doc; name="$filename"rn"
."Content-Type: application/pdf; name="$filename"rn"
."Content-Type: application/docx; name="$filename"rn"
."Content-Transfer-Encoding: base64rn"
."Content-Disposition: attachmentrnrn";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
mail( $mail_to, $subject , $message, $headers );
?>

最新更新