You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHPMailer+SendGrid附加Dompdf生成的PDF报错,求解决方案

解决Dompdf生成PDF直接附加邮件报错「Unknown encoding: pdffilename.pdf」的问题

你遇到的报错「Unknown encoding: pdffilename.pdf」是因为PHPMailer的addStringAttachment参数顺序错误,同时存在变量名不匹配的问题,以下是具体分析和修复方案:

错误原因分析

  1. 参数顺序颠倒:addStringAttachment的正确参数顺序为:
    addStringAttachment(string $string, string $filename, string $encoding = 'base64', string $type = 'application/octet-stream')
    
    你原代码把MIME类型(application/pdf)放在了第二个参数位置(应为文件名),把文件名(pdffilename.pdf)放在了第三个参数位置(应为编码格式),导致PHPMailer误将文件名识别为编码格式,引发错误。
  2. 变量名不匹配:用Dompdf生成的PDF内容变量是$attachthispdf,但添加附件时使用了未定义的$quotepdf,导致传递错误内容。

修复后的完整代码

// Required Libraries
require __DIR__ . "/vendor/autoload.php";
require __DIR__ . "/vendor/phpmailer/phpmailer/src/PHPMailer.php";
require __DIR__ . "/vendor/phpmailer/phpmailer/src/Exception.php";
require __DIR__ . "/vendor/rodrigoq/phpmailersendgrid/src/PHPMailerSendGrid.php";
// Use Classes from Libs
use Dompdf\Dompdf;
use Dompdf\Options;

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


// PDF Gen
$options = new Options;
$options->setChroot(__DIR__);
$options->setIsRemoteEnabled(true);

$dompdf = new Dompdf($options);
$dompdf->setPaper("A4","landscape");

$html = file_get_contents("pdftemplate.html");

// HTML STR_REPLACE to insert POST values into the html file above
$html = str_replace(["{{ date }}", "{{ title }}", "{{ first_name }}", "{{ last_name }}", " {{ email }}", "{{ phone }}", "{{ address_line_1 }}", "{{ address_line_2 }}", "{{ city }}", "{{ county }}", "{{ postcode }}"],
[$date, $title, $first_name, $last_name, $email, $phone, $address_line_1, $address_line_2, $city, $county, $postcode], $html);

$dompdf->loadHTML($html);
$dompdf->render();
$dompdf->addInfo("Title", "PDF File Title");
$attachthispdf = $dompdf->output();

// Now that the PDF is created and ready (I have tested, it generates perfectly fine when saving to server) - I attempt to attach $attachthispdf to my Mail function below

$mail = new PHPMailerSendGrid(true);
try {
  $mail->isSendGrid();
  $mail->SendGridApiKey = 'MYSENDGRID_API_KEY'; // SendGrid API Key.


  //Recipients
  $mail->setFrom('service@mydomain.com', 'Web Service');
  $mail->addAddress($email); // This is $_POST['email'];
  $mail->addReplyTo('contact@mydomain.com', 'Web Service');

  //Attachments - 修正参数顺序和变量名
  $mail->addStringAttachment($attachthispdf, 'pdffilename.pdf', 'base64', 'application/pdf'); 

  //Content
  $mail->isHTML(true); // Set email format to HTML
  $mail->Subject = 'We have received your request';
  $mail->Body = '<html>

<body>
  <div>
    <p>Thank you</p>
    </br>
    <p><strong>Contact Information</strong></p>
    <p>Hey: '.$title.' '.$first_name.' '.$last_name.'</p>
    </br>
    </br>
    <p>Please see the attached document to confirm all your details </p>
  </div>
</body>

</html>';
  $mail->send();
  echo 'Message has been sent.';
  header("Location: http://example.com/thank-you.html"); // I would like to redirect to this specific page upon success
  exit();
}
catch (Exception $e)
{
  echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

关键修复点

  • 附件代码修正为:$mail->addStringAttachment($attachthispdf, 'pdffilename.pdf', 'base64', 'application/pdf');
    • 第一个参数:PDF内容变量$attachthispdf(修正了原变量名错误)
    • 第二个参数:附件文件名pdffilename.pdf
    • 第三个参数:编码格式base64(PDF二进制内容需base64编码才能作为字符串附件)
    • 第四个参数:MIME类型application/pdf

修正后即可将Dompdf生成的PDF直接作为字符串附件添加到邮件,无需保存到服务器。

内容的提问来源于stack exchange,提问作者Devislife

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:21:01