使用 PHPMailer 附加一个 dompdf 文件作为附件,而无需事先将文件保存到磁盘中

问题描述

我想用 PHPMailer 发送一封附有 pdf 文件的电子邮件(实际上是通过 wordpress 中的 wp_mail() 但 WP 使用 PHPMailer)。

pdf 文件是动态的,我使用 dompdf 生成它。

the-pdf-file.PHP

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($HTML);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4','portrait');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF
$dompdf->stream();

但它本身发送的是实际的 PHP 文件,而不是生成的 pdf,这是一个非常合乎逻辑的事情。

我不知道如何强制 PHPMailer 在将文件放入邮件之前“处理”该文件

我是如何附加的:(注意 wp_mail 是一个使用 PHPMailer 的 wordpress 函数

$attachments = array(ABSPATH . '/wp-content/dompdf/generar-pdf.PHP');
$headers[] = 'Content-Type: text/html; charset=UTF-8';
wp_mail('...','Test - '.uniqid(),'Whatever',$headers,$attachments);

一种可能的解决方案是在调用 wp_mail() 函数之前将 pdf 文件保存到磁盘中,如下所示:

$output = $dompdf->output();

file_put_contents(ABSPATH . '/wp-content/dompdf/generados/filename.pdf',$output);

$attachments = array(ABSPATH . '/wp-content/dompdf/generados/filename.pdf');

然后删除文件。但我不知道这是否是一个干净的问题解决方案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)