douyan1972 2017-05-22 22:42
浏览 59
已采纳

梨发送的MIME PDF文件附件未打开

Here's my code for generating a PDF on-the-fly by using MPDF and sending it as attachment. I have removed unnecessary code to make things quickly understandable.

<?php
   require_once "Mail.php";
   require_once "Mail/mime.php";
   include 'mpdf/mpdf.php';
   $server = 'my-domain-name';

   $header = file_get_contents($server.'/pdf-header.php);
   $html = file_get_contents($server.'/pdf-data.php);
   //....Some parameters for mPDF constructor
   $mpdf = new mPDF('en-x','A4','','',$marginLeft, $marginRight, $marginTop, $marginBottom, $marginHeader, $marginFooter);
   // *** Set some properties of $mpdf object
   $mpdf->WriteHTML($html); # Write html to pdf
   $content = $mpdf->Output('', 'S'); // Saving pdf to attach to email 
   $content = chunk_split(base64_encode($content));

   # Set $from, $to, $subject and parameters for SMTP Authentication
   $bodyHTML = '<p>Some HTML Body</p>';
   $bodyTXT = 'Plain Text Body for clients not supporting HTML';

   $headers = array ('From' => $from,   'To' => $to,
                     'Reply-To' => $to, 'Subject' => $subject);
   $smtpParams = array ('host' => $host, 'port' => $port,
                        'auth' => true, 'username' => $emailID,
                        'password' => $password);
   $mime = new Mail_mime("
");
   $mime->setTXTBody($bodyTXT);
   $mime->setHTMLBody($bodyHTML);
   $mime->addAttachment($content, 'application/pdf', 'report.pdf', false, 'base64', 'attachment');
   # Get Mime Body
   $body = $mime->get();
   # Get Mime Headers
   $mimeHeaders = $mime->headers($headers);

   $smtp = Mail::factory('smtp', $smtpParams);
   $mail = $smtp->send($to, $mimeHeaders, $body);
?>

The email is sent successfully to recipient with attachment report.pdf having file size as expected, but the attachment does not open either in any browser not in Adobo Acrobat. The encoding of report.pdf is perhaps wrong.

If I replace-

$content = $mpdf->Output('', 'S'); // Saving pdf to attach to email 
$content = chunk_split(base64_encode($content));

with

$mpdf->Output();

The PDF is output to the browser perfectly, without any errors. That means I am messing-up with something in-

$mime->addAttachment($content, 'application/pdf', 'report.pdf', false, 'base64', 'attachment');

Similar problems have been discussed on StackOverflow but they are for picking up file from the storage and then email it. I have tried that also, but again, the PDF is corrupted.

Could any one please tell me what is wrong with my script?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • duanque2413 2017-05-23 07:50
    关注

    The problem of your code is that you double encode your pdf file. Calling chunk_split and base64_encode is part of Mail_mimes internal work so you don't have to do it. Calling addAttachment with the 'base64' parameter only tells Mail_mime to encode it with base64, not that you have to do this.

    To fix your code, all you have to do is to remove the following line

    $content = chunk_split(base64_encode($content));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?