Error Generating PDF with PHPMailer [closed]

1

I'm using Phpmailer to do email sending but I'd like to know why when I try to create a PDF attachment the file does not open right, with the sending is everything normal, until the pdf is generated, but it does not open. this function below that I'm trying to make funcinar.

     $mail->addStringAttachment(file_get_contents('teste.php'), 'myfile.pdf');
    
asked by anonymous 24.10.2015 / 04:44

1 answer

3

Some things to understand:

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

You must first generate the PDF and get the URL of where the static version of it is saved or the variable where its data is saved, something like:

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

...
return $pdf;

So you should get it like this:

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