【发布时间】: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,它比 phpmail()函数要好得多,也容易得多。 -
PHPMailer的使用方法