我正在尝试编写一个 PHP 脚本来生成 PDF 并通过电子邮件发送它。我的 PDF 生成器可以完美地作为一个独立的 URL,但由于某种原因,当我尝试将脚本通过电子邮件发送到生成的 PFD 时,无法打开收到的文件。这是代码:
include_once('Mail.php');
include_once('Mail/mime.php');
$attachment = "cache/form.pdf";
// vvv This line seems to be where the breakdowns is vvv
file_put_contents( $attachment, file_get_contents( "http://www.mydomain.com/generator.php?arg1=$arg1&arg2=$arg2" ) );
$message = new Mail_mime();
$message->setTXTBody( $msg );
$message->setHTMLBody( "<html><body>$msg</body></html>" );
$message->addAttachment( $attachment );
$body = $message->get();
$extraheaders = array( "From" => $from,
"Cc" => $cc,
"Subject" => $sbj );
$mail = Mail::factory("mail");
$headers = $message->headers( $extraheaders );
$to = array( "Jon Doe <jon@mydomain.com>",
"Jane Doe <jane@mydomain.com>" );
$addresses = implode( ",", $to );
if( $mail->send($addresses, $headers, $body) )
echo "<p class=\"success\">Successfully Sent</p>";
else
echo "<p class=\"error\">Message Failed</p>";
unlink( $attachment );
我标记的行确实在缓存文件夹中生成了一个 PDF 文件,但它不会打开,所以这似乎是一个问题。但是,当我尝试附加一个已经存在的 PDF 文件时,我遇到了同样的问题。我也试过$message->addAttachment( $attachment, "Application/pdf" );
了,好像没什么区别。