Error Generating PDF with Phpmailer

Asked

Viewed 109 times

1

I am using Phpmailer to perform email sending more I would like to know why when I try to create a PDF attachment the file does not open right, With the upload everything is normal, up to the pdf and generated, but does not open. this function below that I am trying to make run.

     $mail->addStringAttachment(file_get_contents('teste.php'), 'myfile.pdf');
  • Fabio, to get around the problem, create a . pdf with the TCPDF class (http://www.tcpdf.org/). This way you will first generate . pdf with TCPDF then attach with Phpmailer class normally as you are doing.

  • to half lost here to join mount the class in phpmaille. would have some example?

  • 1

    The individual working classes managed to generate the PDF with the TCPDF class? It would look like this: $arquivo = $pdf->Output('arquivo.pdf','S'); then yes attach in Phpmailer: $mail->addStringAttachment($arquivo, 'myfile.pdf');

1 answer

3

Some things to understand:

  • Phpmailer does not generate Pdfs, it just sends emails
  • file_get_contents takes the contents of the files and not the response to the execution of a file, so if php test. generates a PDF, when using file_get_contents the php test. will never run and will never process the PDF.

You should first generate the PDF and get the URL from where the statics version of it is saved or the variable where the data from it is saved, something like:

The.php test should set the content in a variable and send using return:

...
return $pdf;

Then you must catch him like this:

$pdfData = require 'teste.php';
$mail->addStringAttachment($pdfData, 'myfile.pdf');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.