6

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

PDF网址就像

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

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

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

下面是我用来附加 pdf 的代码。

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

正在发送消息,但未附加 pdf。它给出以下消息。

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

我需要一些帮助

4

1 回答 1

6

To have the answer right here:

As phpmailer would not auto-fetch the 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 watch out for:

Depending on the server response time, your script might run into timing issues. Also, the fetched data might be pretty large and could cause php to exceed its memory allocation.

于 2014-02-08T11:28:36.740 回答