【问题标题】:Send FPDF File with WordPress wp_mail without saving PDF file使用 WordPress wp_mail 发送 FPDF 文件而不保存 PDF 文件
【发布时间】:2020-04-19 20:41:13
【问题描述】:

我正在尝试使用 FPDF 库创建 PDF 并通过 WordPress wp_mail 功能将其发送到特定的电子邮件地址。重要提示:我不想先将文件保存在我的服务器上。

有很多线程和网站,但没有一个解决方案适合我。

目前我有以下代码:

$email = 'test@mail.com';
$subject = 'Test attachment';
$body = 'This is the body text.';

$separator = md5(time());

$headers = "MIME-Version: 1.0"; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$headers .= "Content-Transfer-Encoding: 7bit";

$pdfdoc = $pdf->Output("test.pdf", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

wp_mail( $email, $subject, $body, $headers, $attachment );

我收到一封没有附件的电子邮件。我也试过:

$pdfdoc = $pdf->Output("test.pdf", "S");
$attachment = array( chunk_split(base64_encode($pdfdoc)));

wp_mail( $email, $subject, $body, $headers, $attachment );

如果我从我的服务器附加图像,该文件将附加以下代码并且工作正常:

$attachment = array( $_SERVER['DOCUMENT_ROOT'] . '/path/to/file/image.png' );

wp_mail( $email, $subject, $body, $headers, $attachment );

所以我唯一的问题是如何生成 PDF 文件并将其附加到电子邮件中。

非常感谢。

【问题讨论】:

    标签: wordpress pdf fpdf


    【解决方案1】:

    我遇到了同样的问题,我通过将文件临时保存在服务器上,然后在发送电子邮件后删除它来解决它。像魅力一样工作。

    这是工作代码:

    $email = 'test@mail.com';
    $subject = 'Test attachment';
    $body = 'This is the body text.';
    
    $separator = md5(time());
    
    $headers = "MIME-Version: 1.0"; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
    $headers .= "Content-Transfer-Encoding: 7bit";
    
    $filename = __DIR__ . "test.pdf"; // You specify the path for the file
    
    $pdf->Output($filename, "F"); // "F" is for saving the file on the server
    
    $attachment = array($filename);
    
    wp_mail( $email, $subject, $body, $headers, $attachment );
    
    unlink($filename); // Deletion of the created file.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 2021-07-06
      • 2019-07-02
      • 1970-01-01
      • 2016-04-11
      • 2014-07-20
      • 1970-01-01
      相关资源
      最近更新 更多