我目前正在尝试用FPDF生成pdf,然后用PHPMailer发送电子邮件。我知道PHPMailer功能正在工作,并且我可以创建pdf。但是当我尝试先将pdf下载到服务器时,输出($pdf,"F"),我得到错误:
警告(2):fopen(/temp-file.pdf): failed to open stream: Permission denied [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");
}
有人能给我一个解决方案吗?谢谢你!
你只需要修改你的权限。如果FPDF不能写入文件,那么PHPMailer就没有什么可发送的,所以它当然不会工作。
或者你可以渲染成一个字符串并附加它——这样就不需要写文件了:
$pdfdoc = $pdf->Output('', 'S');
...
$mail->addStringAttachment($pdfdoc, 'my-doc.pdf');
如果你需要保存文件和发送,使用这个
$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
注意文件位置