6

I am currently trying to generate a pdf with FPDF and then send it in an email with PHPMailer. I know that the PHPMailer functionality is working, and I can create the pdf. But when i try to download the pdf to the server first, output($pdf,"F"), I get the error:

Warning (2): fopen(/temp-file.pdf): failed to open stream: Permission denied [APP/Vendor/fpdf/fpdf.php, line 1025]FPDF error: Unable to create output file: /temp-file.pdf

The pdf creation is very long, so i will just show you me trying to ouptut it.

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");
                    }

Does anyone have a solution for me? Thank you!

Anthony
  • 233
  • 4
  • 14
  • Here is another answered question may be it will help you . http://stackoverflow.com/questions/4353271/email-pdf-attachment-with-php-using-fpdf – Garry Aug 26 '15 at 14:36
  • that stops errors but the pdf isn't sent with the email – Anthony Aug 26 '15 at 14:39

2 Answers2

10

You just need to fix your permissions. If FPDF can't write the file, then there's nothing for PHPMailer to send, so of course it won't work.

Alternatively you can render to a string and attach that instead - this way it doesn't need to write a file:

$pdfdoc = $pdf->Output('', 'S');
...
$mail->addStringAttachment($pdfdoc, 'my-doc.pdf');
Synchro
  • 35,538
  • 15
  • 81
  • 104
0

If you need to save file and send, use this.

   $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

take care of file location.

Subin Thomas
  • 1,408
  • 10
  • 19