[英]HTML2PDF page stores file but is not sent in e-mail
我的代碼有問題,它應該將 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}";
}
這里有幾個問題。 這種組合不起作用:
$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';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.