【问题标题】:HTML2PDF page stores file but is not sent in e-mailHTML2PDF 页面存储文件但不通过电子邮件发送
【发布时间】:2021-05-23 01:20:21
【问题描述】:

我的代码有问题,它应该将 pdf 文件存储在服务器中,然后将其作为电子邮件的附件自动发送给客户端,代码如下:

ob_end_clean(); 
$fileName='CW00'.$commande.'.pdf';
$file = $html2pdf>Output('/home/itscoma/public_html/components/'.$fileName,'F');
try{
$mail = new PHPMailer();
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->CharSet = 'UTF-8';
$mail->Host= "smtp.gmail.com";
$mail->SMTPDebug  = 0;                 
$mail->SMTPAuth   = true;         
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 465;                  
$mail->Username   = "***@***.com";
$mail->Password   = "******";
$mail->From='a***@gmail.com';
$mail->AddAddress('*****@gmail.com');
$mail->AddReplyTo('*****@gmail.com');   
$mail->Subject='sujet';
$mail->Body='message';
$mail->AddAttachment($file);
$mail->Send();
unset($mail);
} catch (Exception $e) {

  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

}

【问题讨论】:

  • 您遇到的错误是什么?文件是否存储在服务器中?您是否前往保存位置并确认文件存在?
  • 看在上帝的份上,不要将您的密码放在公共论坛上!

标签: php phpmailer html2pdf


【解决方案1】:

这里有几个问题。这种组合不起作用:

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 465;                  

Port 更改为 587 以使用该加密模式,将加密模式更改为 PHPMailer::ENCRYPTION_SMTPS 以使用端口 465。

这一行毫无意义:

$file = $html2pdf>Output('/home/itscoma/public_html/components/'.$fileName,'F');

因为您缺少-,所以它应该是$html2pdf->Output

接下来,该方法返回什么?你在$file 中得到了什么?如果它不是保存文件的路径(无论如何你已经知道了),那么将它传递给addAttachment 不会做任何有用的事情。最好检查一下这个调用是否真的有效——目前你只是假设它有效。

如果您检查来自 addAttachment() 的返回值,您会知道这一点,但您不知道,所以您不知道它失败了。

我建议试试这个:

if (!$mail->addAttachment('/home/itscoma/public_html/components/' . $fileName)) {
    echo 'Attachment failed';
}

【讨论】:

    猜你喜欢
    • 2012-11-17
    • 1970-01-01
    • 2012-01-21
    • 2020-03-01
    • 2015-12-07
    • 1970-01-01
    • 2014-09-04
    • 2013-01-16
    • 1970-01-01
    相关资源
    最近更新 更多