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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行