phpmailer从动态网址附加pdf

Ira*_*ana 6 php email phpmailer email-attachments

我正在使用phpmailer发送电子邮件.我有web服务来生成pdf.此pdf不会上传或下载到任何地方.

PDF网址就像

http://mywebsite/webservices/report/sales_invoice.php?company=development&sale_id=2
Run Code Online (Sandbox Code Playgroud)

我需要将此动态 pdf网址附加到我的电子邮件中.我的电子邮件发送服务网址就像

http://mywebsite/webservices/mailservices/sales_email.php
Run Code Online (Sandbox Code Playgroud)

以下是我用来附加pdf的代码.

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

发送消息正在运行,但未附加pdf.它给出了以下信息.

无法访问文件:../ report/sales_invoice.php?company = development&sale_id = 2

我需要一些帮助

小智 7

要在这里得到答案:

由于phpmailer不会自动获取远程内容,因此您需要自己完成.

所以你去:

// 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...
Run Code Online (Sandbox Code Playgroud)

其他一些需要注意的事项:

根据服务器响应时间,您的脚本可能会遇到计时问题.此外,获取的数据可能非常大,可能导致php超出其内存分配.