Phpmailer appends pdf file from dynamic url

I am sending an email using phpmailer . I have a web service for creating pdf. This PDF file does not load and does not load anywhere.

PDF url is similar to

http://mywebsite/webservices/report/sales_invoice.php?company=development&sale_id=2 

I need to attach this dynamic pdf address to my email. My email sending URL is similar to

 http://mywebsite/webservices/mailservices/sales_email.php 

Below is the code that I use to attach pdf.

 $pdf_url = "../report/sales_invoice.php?company=development&sale_id=2"; $mail->AddAttachment($pdf_url); 

The sending message works, but pdf is not attached. He gives below message.

Failed to access file: ../ report / sales_invoice.php? Company = development & sale_id = 2

I need help

+6
source share
1 answer

To get the answer right here:

Since phpmailer will not automatically retrieve remote content, you need to do it yourself.

So you go:

 // we can use file_get_contents to fetch binary data from a remote location $url = 'http://mywebsite/webservices/report/sales_invoice.php?company=development&sale_id=2'; $binary_content = file_get_contents($url); // You should perform a check to see if the content // was actually fetched. Use the === (strict) operator to // check $binary_content for false. if ($binary_content === false) { throw new Exception("Could not fetch remote content from: '$url'"); } // $mail must have been created $mail->AddStringAttachment($binary_content, "sales_invoice.pdf", $encoding = 'base64', $type = 'application/pdf'); // continue building your mail object... 

Some other things to consider:

Depending on the server response time, your script may run into time problems. In addition, the extracted data can be quite large and can lead to php exceeding the memory allocation.

+6
source

Source: https://habr.com/ru/post/949585/


All Articles