I'm trying to generate EML files from PHP. Is there any library that will allow me to easily create them? I could find some ActiveX component on the internet but would rather use something more portable.
-
Do you want to create it from scratch, or after reading email wit imap functions? Maybe imap_savebody() can help - http://www.csschat.com/archive/index.php/t-3287.html ? – BartekR Jan 08 '12 at 09:34
-
Yes I need to create it from scratch. I basically have an email, title, body and attachment and need to create the EML for that. – laurent Jan 08 '12 at 09:41
3 Answers
I ended up building the MIME message myself using this kind of template, where each field is replaced by a TEMPLATE_<name>
variable:
From: TEMPLATE_FROM_ADDRESS
MIME-Version: 1.0
To: TEMPLATE_TO_ADDRESS
Subject: TEMPLATE_SUBJECT
Content-Type: multipart/mixed; boundary="080107000800000609090108"
This is a message with multiple parts in MIME format.
--080107000800000609090108
Content-Type: text/plain
TEMPLATE_BODY
--080107000800000609090108
Content-Type: application/octet-stream;name="TEMPLATE_ATTACH_FILENAME"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="TEMPLATE_ATTACH_FILENAME"
TEMPLATE_ATTACH_CONTENT
--080107000800000609090108
Then creating the final message is quite simple using str_replace
:
$content = file_get_contents("Template.eml");
$content = str_replace("TEMPLATE_FROM_ADDRESS", $fromEmail, $content);
$content = str_replace("TEMPLATE_TO_ADDRESS", $toEmail, $content);
// etc. for each template parameter
// Also don't forget to base64_encode the attachment content;
$content = str_replace("TEMPLATE_ATTACH_CONTENT", base64_encode($attachContent), $content);
Additional info about file attachment in this post: Attachment name and file extension not working in email *.eml
Edit (2018): Since this answer was written it seems it's been copied and pasted a bit everywhere, the template in particular. To avoid any conflict with other MIME data, you should make sure that the boundary "080107000800000609090108" is unique - it's a string of random characters no longer than 70 characters.

- 88,262
- 77
- 290
- 428
I think you don't need a library. It's just plain text (e.g. http://bitdaddys.com/example1.eml)
Date: Sat, 12 Aug 2006 14:25:25 -0400
From: John Doe <jdoes@someserver.com>
Subject: BitDaddys Software
To: sales@bitdaddys.com
Dear BitDaddys Corp.,
We have added your software to our approved list.
Thank you for your efforts.
Sincerely,
John Doe
Some Server Company
You can just output text with headers and save it using fwrite. For attachments use base64_encode()
as stated here

- 3,827
- 3
- 24
- 33
-
3The important thing is that it should follow SMTP semantics - i.e. CRLF at the end of each header and a blank line (terminated by CRLF) between the headers and the body. Body should be transfer encoded as 7bit ascii (or quoted printable). – symcbean Jan 08 '12 at 13:02
-
1
Use imap_savebody (part of the imap library http://us1.php.net/manual/en/function.imap-savebody.php) with a null $part_number. It creates a beautiful .eml file with one line of code with the entire message (null $part_number = all parts... not documented but works).
the other two solutions depend on the format of the email (only one attachment and no html section in the first solution, and only text email in the second).
imap_savebody creates a perfect .eml file no matter what the format of the incoming email is (as long as it's RFC-complaint of course).

- 116
- 3