php - 附件名称和文件扩展名在电子邮件 *.eml 中不起作用

标签 php email zend-framework email-attachments eml

我想创建包含多个附件的.eml 格式的电子邮件文件。附件已生成且附件内容正确。但附件始终以 .dat 格式出现,文件名为“ATT00001”、“ATT0002”等

目前我正在遵循 this stackoverflow question 中给出的解决方案我的代码如下

PHP

   foreach($reports as $pdf){
        $attachment = file_get_contents("auto_report_attachments\\Template_Attachment.eml");
        $attachment = str_replace("TEMPLATE_MIME_TYPE", $pdf['type'], $attachment);
        $attachment = str_replace("TEMPLATE_FILE_NAME", $pdf['filename'], $attachment);
        $attachment = str_replace("TEMPLATE_ATTACHMENT_CONTENT", base64_encode($pdf['file']), $attachment);

        $content .= $attachment;
        unset($attachment);
    }

模板附件

--080107000800000609090108
Content-Type: "TEMPLATE_MIME_TYPE"
name="TEMPLATE_FILE_NAME"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="TEMPLATE_FILE_NAME"

TEMPLATE_ATTACHMENT_CONTENT

$content 是上面链接中描述的主要电子邮件 header 和正文。我的 .eml 文件看起来像;

MIME-Version: 1.0
Date: Tue, 16 Apr 2013 09:03:37 +0100
From: sender@emailhost.com
To: recipient@emailhost.com
Subject: Email subject
Content-Type: multipart/mixed; boundary="080107000800000609090108"

This is a message with multiple parts in MIME format.

--080107000800000609090108
Content-Type: text/html

<p><strong>Project Name: Some Project and the body continues...</p>



--080107000800000609090108
Content-Type: "application/pdf"
name="AM22831 -  - Cover Sheet.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="AM22831 -  - Cover Sheet.pdf"

JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovQ29udGVudHMgNCAwICiUlRU9GCg==



--080107000800000609090108
Content-Type: "application/pdf"
name="AM22831 -  - Manufacturing Status.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="AM22831 -  - Manufacturing Status.pdf"

cSAxMTMuMzkgMCAwIDMwLjUzIDE0LjE3IDU1MC41OCBjbSAvSTEgRG8gUQpxIDAuMDAwIDAuMDAwIDEuMDAwIHJnIEJUIDEzMC4zOSRU9GCg==

--080107000800000609090108

上面的 base64 内容在通过选择要使用 pdf Reader 打开的文件打开时,在 pdf 文件中提供了正确的内容。但文件不是 .pdf 格式。 .xls.doc 和所有其他文件类型也是如此。所有文件均采用 .dat 格式,具有标准命名而不是指定名称。

请帮助我解决此附件问题,该附件来自具有指定文件名的所需文件类型

注意:示例 .eml 文件中的 base64 内容被截断

最佳答案

我自己找到了答案..!! Template_Attachment 的头部应该如下所示

--080107000800000609090108
Content-Type: TEMPLATE_MIME_TYPE;name="TEMPLATE_FILE_NAME"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="TEMPLATE_FILE_NAME"

TEMPLATE_ATTACHMENT_CONTENT

在这种情况下,080107000800000609090108 是边界,在所有附件的末尾,应该有另一个 080107000800000609090108 来结束电子邮件。

希望有一天这会对某人有所帮助:-)

PS:所以实际的 .eml 文件看起来像下面这样完美地工作

MIME-Version: 1.0
Date: Tue, 16 Apr 2013 09:03:37 +0100
From: sender@emailhost.com
To: recipient@emailhost.com
Subject: Email subject
Content-Type: multipart/mixed; boundary="080107000800000609090108"

This is a message with multiple parts in MIME format.

--080107000800000609090108
Content-Type: text/html

<p><strong>Project Name: Some Project and the body continues...</p>

--080107000800000609090108
Content-Type: application/pdf;name="AM22831 Cover Sheet.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="AM22831 Cover Sheet.pdf"

JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovQ29udGVasdsDFDffjMBakdjKJHBzdHlsZT0iY29=

--080107000800000609090108
Content-Type: application/excel;name="AM22831 Manufacturing Status.xls"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="AM22831 Manufacturing Status.xls"

DQoNCjx0YWJsZSBib3JkZXI9IjAiPg0KPHRyPg0KPHRkIGNvbHNwYW49IjMiIHJvd3NwYW49IjIiIGFsaWduPSJjZW50ZXIiPg0KICAgIDxoMSBzdHlsZT0iY29=

关于php - 附件名称和文件扩展名在电子邮件 *.eml 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16032713/

相关文章:

jquery - 尝试使用 jQuery 为 Zend Framework 找到 AJAX 文件上传解决方案

php - MySQL PHP 将查询中的单词与数据库列匹配

php - 数据库插入时的 SQL 查询错误

PHP:拆分字符串

php - 当 iframe 内容准备就绪时,从父页面访问 iframe 内容

css - 浏览器/网络邮件客户端中的默认打印样式

email - 如何确认邮件来源

php - 选择至少有一个参数不同的条目,传递数组

java - 在 java 中使用 Gmail API 发送电子邮件

php - 如何将 id 属性添加到 Zend Framework 表单?