PHP library for generating eml email files?

I am trying to create eml files from php. Is there a 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.

+6
source share
3 answers

In the end, I created a MIME message using this type of template, where each field is replaced by the variable TEMPLATE_<name> :

 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 pretty 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 information on attaching a file in this message: The attached name and file extension do not work in * .eml email

+8
source

I think you do not need a library. This is just 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 simply output text with headers and save it with fwrite. For applications, use base64_encode() as indicated here

+4
source

Use imap_savebody (part of the imap library http://us1.php.net/manual/en/function.imap-savebody.php ) with zero $ part_number. It creates a beautiful .eml file with one line of code with the whole message (null $ part_number = all parts ... not documented, but works).

the other two solutions depend on the email format (only one attachment and not the html section in the first solution, but only a text message in the second).

imap_savebody creates the perfect .eml file regardless of the format of the incoming message (provided that it is an RFC complaint of course).

+2
source

All Articles