dtd14883 2017-04-30 17:16
浏览 60
已采纳

通过PHPMailer()发送的HTML2PDF输出

I'm using html2pdf to convert a html page to a pdf and I want to send it as a mail. Using the code below, I was able to create the pdf and display it properly on the page (using Output('name.pdf')). I found that in order to send this pdf as attachment in a mail, I need to add a second argument as true or as 'S', so here is the code:

$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;
}

Also, to add, I set the sendmail on wamp. Whenever I access the page, I get success and the mail is sent successful, now my problem is the attachment part, because I'm getting the email with body and subject, but there is no attachment pdf to it. Did I do something wrong on the output part? (both true and 'S' give the same thing). Thanks. PS: I'm using yahoo as smtp to send email and gmail to receive.

展开全部

  • 写回答

1条回答 默认 最新

  • dream1849 2017-04-30 18:16
    关注

    The $pdf output from html2pdf is a blob (or binary?), it is not saved in your server. The first parameter of addAttachment() method expect a path not binary. The method you should have called is addStringAttachment().

    $mail->addStringAttachment($pdf, 'file.pdf');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?