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 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?