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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题