【问题标题】:php Mail_MIME - Generating and Attaching PDFphp Mail_MIME - 生成和附加 PDF
【发布时间】:2014-01-04 11:32:19
【问题描述】:

我正在尝试编写一个 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-&gt;addAttachment( $attachment, "Application/pdf" );,好像没什么区别。

【问题讨论】:

  • 能否不先将数据直接发送到 addAttachment,而不先将其写入文件?
  • 你的意思是像$message-&gt;addAttachment( file_get_contents( "http://www.mydomain.com/generator.php?arg1=$arg1&amp;arg2=$arg2" ) );这样的东西吗?不知道,我可以试试但是,我得出的结论是我的 php.ini 中存在问题。
  • 是的。无论如何,先把它放在一个变量中,但你明白了。
  • 没有骰子。 =/ 我很确定它一定是阻塞 file_get_contents() 的 ini 问题。但是我想出了一个更好的解决方案。我修改了 generator.php 文件并将其转换为函数定义。所以我有include_once('generator.php');$attachment = my_pdf_generator($arg1, $arg2);

标签: php pdf attachment mail-mime


【解决方案1】:

通常,Web 服务器目录应该锁定写权限。这可能就是您遇到file_put_contents('cache/form.pdf') 问题的原因。

// A working example: you should be able to cut and paste, 
// assuming you are on linux.
$attachment = "/var/tmp/Magick++_tutorial.pdf";
file_put_contents($attachment, file_get_contents( 
     "http://www.imagemagick.org/Magick++/tutorial/Magick++_tutorial.pdf"));

尝试将保存 pdf 的位置更改为允许所有人读写权限的目录。还要确保此目录不在您的 Web 服务器上。

也尝试改变以下三件事

来自

$message = new Mail_mime();

// you probably don't need this the default is
// $params['eol'] - Type of line end. Default is ""\r\n""
$message = new Mail_mime("\r\n");

来自

$extraheaders = array(  
       "From"      => $from,
       "Cc"        => $cc,
       "Subject"   => $sbj,
     );

$extraheaders = array(  
        "From"      => $from,
        "Cc"        => $cc,
        "Subject"   => $sbj,
        'Content-Type' => 'text/html'
    );

来自

$message->addAttachment($attachment);

// the default second argument is $c_type = 'application/octet-stream'
$isAttached = $message->addAttachment($attachment, 'aplication/pdf');
if ($isAttached !== true) {
    // an error occured
    echo $isAttached->getMessage();
}

而且你总是想确保你打电话

$message->get();

之前

$message->headers($extraheaders);

否则整个事情都行不通

【讨论】:

  • 不,我仍然收到来自 Adob​​e 的消息“Adobe Reader 无法打开 'form.pdf',因为它不是受支持的文件类型或文件已损坏(例如,它是作为电子邮件附件发送但未正确解码)。”
  • 你能在cahce目录下用阅读器打开pdf吗?
  • 不,这就是为什么我认为那条线路有问题。虽然,我在通过电子邮件发送已经存在的 PDF 文件时也遇到了问题。
【解决方案2】:

我很确定这一定是阻止 file_get_contents() 的 ini 问题。但是我想出了一个更好的解决方案。我修改了 generator.php 文件并将其转换为函数定义。所以我有:

include_once('generator.php');
$attachment = "cache/form.pdf";
file_put_contents( $attachment, my_pdf_generator( $arg1, $arg2 ) );
...
$message->addAttachment( $attachment, "application/pdf" );

这样我就不需要先写文件了。它工作得很好(虽然我仍然在使用 Outlook/Exchange Server 时遇到一些小问题,但我认为这是一个基本上不相关的问题)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    相关资源
    最近更新 更多