【发布时间】:2017-09-28 12:15:58
【问题描述】:
我正在使用 html2pdf 将 html 页面转换为 pdf,我想将其作为邮件发送。使用下面的代码,我能够创建 pdf 并在页面上正确显示(使用 Output('name.pdf'))。我发现为了将这个pdf作为附件发送到邮件中,我需要添加第二个参数为true或'S',所以这里是代码:
$pdf = '';
try
{
// init HTML2PDF
$html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8', array(0, 0, 0, 0));
// display the full page
$html2pdf->pdf->SetDisplayMode('fullpage');
// get the HTML
ob_start();
include('invoice_html.php');
$content = ob_get_clean();
// convert
$html2pdf->writeHTML($content);
// send the PDF
$pdf = $html2pdf->Output('', true);
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
$mail = new PHPMailer();
$mail->setFrom('senderSMTP@yahoo.com', 'sender');
$mail->addAddress('test@gmail.com', 'test');
$mail->Subject = 'TestMail';
$mail->addAttachment($pdf, 'file.pdf');
$mail->Body = 'TestMessage';
if($mail->send())
{
echo 'success';
}
else
{
echo $mail->ErrorInfo;
}
另外,我将 sendmail 设置为 wamp。每当我访问该页面时,我都会成功并且邮件发送成功,现在我的问题是附件部分,因为我收到了带有正文和主题的电子邮件,但没有附件 pdf。我在输出部分做错了吗? (true 和 'S' 给出相同的结果)。谢谢。 PS:我使用 yahoo 作为 smtp 发送电子邮件和 gmail 接收。
【问题讨论】: