MPDF电子邮件附件发送空白PDF

时间:2013-10-31 22:49:49

标签: php pdf phpmailer swiftmailer mpdf

我已成功使用mpdf生成PDF,我已通过下载PDF进行验证。但是,当我将PDF作为电子邮件附件发送时,我收到一个空白的PDF,其中包含Adobe Reader的“Out of Memory”错误。以下是我的代码:

<?php

include("MPDF57/mpdf.php");

ob_start();
include "Receipt_Template_2.php"; 
$template = ob_get_contents();
ob_end_clean();

$mpdf=new mPDF('','A4','','',32,25,27,25,16,13,'L'); 

mpdf->WriteHTML($template);

$content = $mpdf->Output($template, 'S');

$content = chunk_split(base64_encode($content));
$mailto = 'sample@sample.com';
$from_name = 'KIREA';
$from_mail = 'NoReply@kirea.ca';
$uid = md5(uniqid(time()));
$subject = 'KIREA Donation Receipt';
$message = "Thank you for your donation!\n\nAttached is the receipt concerning the     donation. If you have any questions, please e-mail us at receipts@kirea.ca";;
$filename = $pdfName;

$header = "From: ".$from_name." <".$from_mail.">\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\n";
$header .= "Content-Transfer-Encoding: 7bit\n";
$header .= $message."\n\r\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
$header .= $content."\n\n";
$header .= "--".$uid."--";

$is_sent = @mail($mailto, $subject, "", $header);

$mpdf->Output();
exit;

?>

对于PDF作为电子邮件附件发送后为什么结尾为空,有什么想法吗?谢谢。

2 个答案:

答案 0 :(得分:1)

mpdf->WriteHTML($template);
$content = $mpdf->Output($template, 'S');

你错了,你没有拿到你的数据写的对象

请用上面的代码替换以获得正确的结果。

$pdfdata=mpdf->WriteHTML($template);
$content = $mpdf->Output('' , 'S');

在您的电子邮件中使用$content

答案 1 :(得分:0)

如果你可以使用swiftmailer,你可以很容易地将MPDF生成的PDF附加到电子邮件中,如下所示:

<?php
require_once $swift_mailer_path.'swift_required.php';

$transporter = Swift_SmtpTransport::newInstance($smtp_host, $smtp_port, $smtp_protocol)
  ->setUsername($smtp_username')
  ->setPassword($smtp_password');

$mailer = Swift_Mailer::newInstance($transporter);

$message = Swift_Message::newInstance('Email Subject')
  ->setFrom(array($from_email => $from_name))
  ->setTo($to_email)
  ->setBody($email_body);

$attachment = Swift_Attachment::newInstance($mpdf->Output($pdf_path, "S"), $pdf_file_name, 'application/pdf');
$message->attach($attachment);  

$message->setContentType("text/html");

$result = $mailer->send($message);
?>

这是Reference