How to use TCPDF with PHP mail function

╄→尐↘猪︶ㄣ 提交于 2019-12-03 08:38:33

You have two choices. You can save the PDF to a file and attach the file or else output it as a string. I find the string output is preferable:

$pdfString = $pdf->Output('dummy.pdf', 'S');

The file name is ignored since it just returns the encoded string. Now you can include the string in your email. I prefer to use PHPMailer when working with attachments like this. Use the AddStringAttachment method of PHPMailer to accomplish this:

$mailer->AddStringAttachment($pdfString, 'some_filename.pdf');

I tried several alternatives. Only way that worked was when I saved the PDF to a folder and then email it.

$pdf->Output("folder/filename.pdf", "F"); //save the pdf to a folder
  require_once('phpmailer/class.phpmailer.php'); //where your phpmailer folder is
$mail = new PHPMailer();                    
$mail->From = "email.com";
$mail->FromName = "Your name";
$mail->AddAddress("email@yahoo.com");
$mail->AddReplyTo("email@gmail.com", "Your name");               
$mail->AddAttachment("folder/filename.pdf");      // attach pdf that was saved in a folder
$mail->Subject = "Email Subject";                  
$mail->Body = "Email Body";
if(!$mail->Send())
{
       echo "Message could not be sent. <p>";
       echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
       echo "Message sent";
} 
echo 'sent email and attachment';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!