【发布时间】:2015-12-10 02:34:24
【问题描述】:
我在发送带有 PDF 附件的电子邮件时遇到问题,并且消息内容是 html 格式。请查看我的以下代码。 HTML 电子邮件工作正常。但问题出在pdf附件中。
<?php
$random_hash = md5(date('r', time()));
$headers = "From: ".$from."\r\nReply-To: ".$from;
$headers .= "\r\nContent-Type: text/html; boundary=\"PHP-mixed-".$random_hash."\"";
foreach($summaryArray as $summaryArrayValue)
{
$file = 'pdf_directory/'.$summaryArrayValue['result_filename'].'.pdf';
$fileName = $summaryArrayValue['result_filename'];
$attachment = chunk_split(base64_encode(file_get_contents($file)));
$message.=<<<EOD
Content-Type: application/octet-stream; name="{$fileName}" // tried with both application/octet-stream and application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment
{$attachment}
--PHP-mixed-{$random_hash}--
EOD;
}
$mail_sent = mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
【问题讨论】:
标签: php email pdf sendmail email-attachments