Attachment name and file extension not working in email *.eml
Asked Answered
M

1

6

I want to create email files in .eml format with multiple attachments in. The attachments are generated and the content of the attachments are correct. But the attachments always come in .dat format and the file names go as "ATT00001", "ATT0002".. etc

Currently I am following the solution given in this stackoverflow question and my code is as follows

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);
    }

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

$content is the main email header and body as described in the above link. My .eml file looks like;

MIME-Version: 1.0
Date: Tue, 16 Apr 2013 09:03:37 +0100
From: [email protected]
To: [email protected]
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

The above base64 content gives the right content in the PDF file when the file opened by selecting to open with a PDF Reader. But the files do not come in .pdf format. Same happens for .xls, .doc and all other file types. All the files come in .dat format with standard naming rather than the specified names.

Please help me to solve this attachments come in the required file type with specified file name.

NB: The base64 content in the sample .eml file is truncated

Melpomene answered 16/4, 2013 at 8:55 Comment(0)
M
4

I found the answer my self..!! The header of Template_Attachment should be as follows

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

TEMPLATE_ATTACHMENT_CONTENT

Where 080107000800000609090108 is the boundary in this case and at the end of all attachments, there should be another 080107000800000609090108 to end the email.

Hope this would be helpful to somebody someday :-)

PS: so the actual .eml file would look like as follows which works perfect as it should do

MIME-Version: 1.0
Date: Tue, 16 Apr 2013 09:03:37 +0100
From: [email protected]
To: [email protected]
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=
Melpomene answered 16/4, 2013 at 13:58 Comment(2)
This is not working.. Could you please tell which outlook client you used to open this attachment?Psychogenesis
I developed this 7 years ago and I just checked whether this code is still working. It is actually still working and I am using MS Outlook 2010 to open the eml files. attachments can be docx, xlsx, pdf or anything like that and they open in their respective applications.Melpomene

© 2022 - 2024 — McMap. All rights reserved.