返回

在 PHPMailer Sendgrid 中使用 DomPDF HTML 渲染时如何添加附件?

php

在 PHPMailer Sendgrid 中使用 DomPDF HTML 渲染添加附件

当使用 PHPMailer Sendgrid 库发送电子邮件时,附加由 DomPDF HTML 渲染生成的 PDF 文件是一个常见的需求。然而,在尝试此操作时,你可能会遇到 "未知编码:pdffilename.pdf" 的错误。

理解错误

此错误表示电子邮件客户端无法识别你试图附加的文件的编码方式。

解决方法

要解决此错误,你需要使用正确的编码将 PDF 附件附加到电子邮件。以下是步骤:

1. 将 PDF 转换为 base64 编码:

PHPMailer Sendgrid 要求文件编码为 base64,因此你需要将 PDF 转换为 base64 格式:

$attachthispdf = base64_encode($attachthispdf);

2. 使用 addStringAttachment 添加附件:

现在,使用 addStringAttachment 方法将经过 base64 编码的 PDF 添加到电子邮件:

$mail->addStringAttachment($attachthispdf, 'application/pdf', 'pdffilename.pdf');

代码示例

以下是优化后的完整代码,其中包括 base64 编码和 addStringAttachment 方法:

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 Dompdf\Dompdf;
use Dompdf\Options;

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

$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(["{{ 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();

$attachthispdf = base64_encode($attachthispdf);

$mail = new PHPMailerSendGrid(true);
try {
  $mail->isSendGrid();
  $mail->SendGridApiKey = 'MYSENDGRID_API_KEY';

  $mail->setFrom('[email protected]', 'Web Service');
  $mail->addAddress($email); 
  $mail->addReplyTo('[email protected]', 'Web Service');

  $mail->addStringAttachment($attachthispdf, 'application/pdf', 'pdffilename.pdf'); 

  $mail->isHTML(true);
  $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");
  exit();
}
catch (Exception $e)
{
  echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

注意事项

  • 确保将你的 SendGrid API 密钥替换为 MYSENDGRID_API_KEY
  • http://example.com/thank-you.html 替换为你希望成功发送邮件后重定向到的 URL。
  • 此代码可能需要额外的修改和优化以满足你的特定需求。

常见问题解答

Q1:如何将电子邮件主题行中的变量替换为实际值?

A1:使用以下语法将变量替换为实际值:$mail->Subject = 'Subject: ' . $variable;

Q2:如何向电子邮件中添加多个附件?

A2:使用 addAttachment 方法多次添加附件:$mail->addAttachment($pathToFile, $fileName, $encoding, $type);

Q3:如何设置发件人和收件人的名称?

A3:使用以下语法设置发件人和收件人的名称:$mail->setFrom('email@address.com', 'Sender Name');$mail->addAddress('email@address.com', 'Recipient Name');

Q4:如何使用 SMTP 服务器发送电子邮件?

A4:在 PHPMailer 对象中启用 SMTP,如下所示:$mail->isSMTP();

Q5:如何在电子邮件中嵌入图像?

A5:使用以下语法嵌入图像:$mail->addEmbeddedImage($pathToFile, $cid, $name, $encoding, $type);

结论

通过遵循本文中的步骤,你将能够在使用 PHPMailer Sendgrid 库时附加 DomPDF HTML 渲染的 PDF 附件。解决未知编码错误并了解如何在电子邮件中嵌入附件至关重要,以便有效地发送信息丰富且专业的电子邮件。