PHPMailer + HTML2PDF:pdf无效和pj

问题描述

早上好, 我想用HTML2PDF附加根据表单创建的PDF,然后通过PHMailer发送。 一切正常,电子邮件运行顺利,我设法通过将PDF保存在硬盘上来创建PDF。 但是,当我尝试将其附加到我的电子邮件中时,pj中的pdf很好,但是我无法打开它。 enter image description here

它与我本地创建的pdf大小相同。 我遵循了这两个库的教程和Wiki,我想^^ 这是我的PHPMailer代码:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require './vendor/phpmailer/phpmailer/src/Exception.php';
require './vendor/phpmailer/phpmailer/src/PHPMailer.php';
require './vendor/phpmailer/phpmailer/src/SMTP.php';
require './vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';

try {
    //Server settings
    $mail->SMTPDebug = 0;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = '******';             // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '*****';                         // SMTP username
    $mail->Password   = '*****';                    // SMTP password
    //$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = ****;                                    // TCP port to connect to,use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('****@****.com','****');
    $mail->addAddress($to);     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo($shop_email,'Votre magasin');
    //$mail->addCC('cc@example.com');

    $mail->addBCC($shop_email);

    // Attachments
    $mail->addStringAttachment($pdf_done,'myPdf.pdf');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg','new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;
    $mail->AltBody = strip_tags($message);

    $mail->send();
    echo '';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

?>

这是HTML2PDF的代码:

ob_start();
// --> mon code HTML de creeation du PDF
$content = ob_get_clean();

require_once dirname(__FILE__).'/../vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;

try{
    $pdf = new \Spipu\Html2Pdf\Html2Pdf('P','A4','fr');
    $pdf->writeHTML($content);

    $pdf_done = $pdf->output('myPdf.pdf','S');

}catch(\Spipu\Html2Pdf\Exception\Html2PdfException $e){
    die($e);
}

?>

我尝试在addStringAttachment中添加“,'base64','应用程序/ pdf'”,但它没有任何改变。 你知道我的错误吗?

谢谢大家

解决方法

一次调试一件事。首先手动下载PDF,并确保它看起来正确–如果PDF入门不正确,则通过电子邮件发送并不会有所改善。接下来检查它是否可以从PHP读取-例如,使用readfile()来提供它。然后,当您尝试附加它时,请像下面这样检查对addAttachment()的调用的返回值:

if (!$mail->addAttachment('/path/to/myPdf.pdf')) {
    die('could not read PDF');
}

还要注意addAttachmentaddStringAttachment之间的区别;您要添加已保存到磁盘的文件,因此需要第一个文件,而不是第二个文件。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...