【问题标题】:Send FPDF document with PHPMailer;使用 PHPMailer 发送 FPDF 文档;
【发布时间】:2015-11-20 15:56:36
【问题描述】:

我目前正在尝试使用 FPDF 生成 pdf,然后使用 PHPMailer 将其发送到电子邮件中。我知道 PHPMailer 功能正在运行,我可以创建 pdf。但是当我尝试先将pdf下载到服务器时,输出($pdf,“F”),我得到了错误:

警告 (2): fopen(/temp-file.pdf): 无法打开流: 权限被拒绝 [APP/Vendor/fpdf/fpdf.php, line 1025]FPDF 错误: 无法创建输出文件: /temp -file.pdf

pdf 的创建很长,所以我将向您展示我尝试输出它的过程。

FPDF

$pdfoutputfile = 'temp-folder/temp-file.pdf';
$pdfdoc = $pdf->Output($pdfoutputfile, 'F');

PHPMailer

$mail = new phpmailer;
                    $mail->SetFrom("info@company.com","Company");
                    $mail->AddAddress($to);
                    $mail->Subject  = "Invoice $id";      
                    $body .= "This is an automatically generated message from Company \n";
                    $mail->Body     = $body;


                    $mail->AddAttachment($pdfoutputfile, 'my-doc.pdf');
                    if(!$mail->Send()) {
                        $this->Session->setFlash("Invoice was not sent");
                        echo 'Mailer error: ' . $mail->ErrorInfo;
                        } else {
                        $this->Session->setFlash("Invoice was sent");
                    }

有人可以帮我解决吗? 谢谢!

【问题讨论】:

标签: php email phpmailer fpdf


【解决方案1】:

如果你需要保存文件并发送,使用这个。

   $file = basename("test");    //create file
    $file .= '.pdf';    //change extension of file to .pdf
    $pdf->Output($file, 'F');   //save file
  ..... 
    $mail->AddAttachment("test.pdf");   //add attachment

注意文件位置。

【讨论】:

    【解决方案2】:

    您只需要修复您的权限。如果FPDF不能写文件,那么PHPMailer就没有东西可以发送了,所以当然不行。

    或者,您可以渲染到一个字符串并附加它 - 这样它就不需要编写文件:

    $pdfdoc = $pdf->Output('', 'S');
    ...
    $mail->addStringAttachment($pdfdoc, 'my-doc.pdf');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      相关资源
      最近更新 更多