dtd14883
2017-05-01 01:16
浏览 55
已采纳

通过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.

图片转代码服务由CSDN问答提供 功能建议

我正在使用html2pdf将html页面转换为pdf,我希望将其作为邮件发送。 使用下面的代码,我能够创建pdf并在页面上正确显示(使用Output('name.pdf'))。 我发现为了将这个pdf作为附件发送到邮件中,我需要添加第二个参数为true或'S',所以这里是代码:

   $ pdf =''; 
 
try 
 {
 // init init HTML2PDF 
 $ html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(0  ,0,0,0)); 
 
 //显示整页
 $ html2pdf-> pdf-> SetDisplayMode('fullpage'); 
 
 //获取HTML 
 ob_start(  ); 
 include('invoice_html.php'); 
 $ content = ob_get_clean(); 
 
 // convert 
 $ html2pdf-> writeHTML($ content); 
 
 //发送 PDF 
 $ pdf = $ html2pdf->输出('',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'adccess'; 
} 
else 
 {
 echo $ mail-> ErrorInfo; 
} 
   
 
 

另外,要添加,我在wamp上设置sendmail。 每当我访问页面时,我都会获得成功并且邮件发送成功,现在我的问题是附件部分,因为我收到了带有正文和主题的电子邮件,但是没有附件pdf。 我在输出部分做错了吗? (真实和'S'都给出了相同的东西)。 谢谢。 PS:我使用yahoo作为smtp来发送电子邮件和gmail来接收。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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');
    
    打赏 评论

相关推荐 更多相似问题