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