【问题标题】:Sending email with pdf attachment in php在php中发送带有pdf附件的电子邮件
【发布时间】:2014-05-29 15:37:40
【问题描述】:

我是 php 新手。我需要发送带有 pdf 附件的电子邮件。我可以发送带有附件的电子邮件。但无法打开pdf。我收到这样的错误

将信息存储到数据库(完成),向员工发送一封包含新客户信息的电子邮件(完成),并向客户发送一封带有 pdf 文件附件的“感谢消息”电子邮件(不工作)。我的意思是,客户确实收到了一封电子邮件,但是当他/她打开 pdf 文件时,我收到以下错误消息:

“Acrobat 无法打开 'file_name',因为它不是受支持的文件类型或文件已损坏(例如,它作为电子邮件附件发送并且未正确解码)...”

如果有人能帮我解决这个问题,那就太好了。谢谢!

这是我的代码:

$to = 'form@kronova.in, ' . $Email;
$subject = 'ABC :: Admission Form Details';
$repEmail = 'form@kronova.in';

$fileName = 'ABC-Admission.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());

$headers = 'From: Principal abc <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "Thanks for filling online application form. Your online admission registration number is E0000". mysql_insert_id() . "." .$eol;

$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;

$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";

if (mail($to, $subject, $message, $headers)){
echo "Email sent";
}

else {
echo "Email failed";

【问题讨论】:

  • 尝试将$attachment = chunk_split($fileatt); 替换为$attachment = chunk_split(base64_encode($fileatt));。除此之外,使用 PHPMailer,它比 php mail() 函数要好得多,也容易得多。
  • PHPMailer的使用方法

标签: php email pdf


【解决方案1】:

我建议使用框架为您完成繁重的工作。看看 Zend 框架 2。你想要实现的目标可以用几行代码来完成。

文档非常详尽。查看此页面上的快速入门指南。

http://framework.zend.com/manual/2.3/en/modules/zend.mail.message.html

正如其他人所建议的那样,直接使用邮件功能既麻烦又容易出错,而且只会给您带来更多问题。

【讨论】:

    【解决方案2】:

    只需将application/pdf 更改为application/octet-stream

    【讨论】:

      猜你喜欢
      • 2020-11-20
      • 2014-02-28
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      相关资源
      最近更新 更多