How to export an email into an eml file?

LukePa

New member
Hi:

For backup purpose, I want to export some of my emails into eml files.

Using
parse_email_parts($mbox, $emailid, $structure, 0, $message, $html_message, $attachments, $found_html), I get $message, which is the email with the headers and the body.
When I save $message into an eml file, I can read the email fine but the attachments are missing.
I can extract individually the attachments but what I want is having them part of the eml file.

How can I fix that?

Thanks.
 
I'm not sure I understand your question. I send requests to an IMAP server, and I analyse and parse the response through the PHP imap collection of functions. What I want to achieve is export the email into a file, using the standard eml format. I could do that directly from an email client but I want it done automatically.
 
you need to add the attachments in the .eml file, please check an example .eml file. For each attachment you need to add the dedicated part with encoded content:
Code:
--------------<attachment's part id>
Content-Type: text/plain; charset=UTF-8;
 name="<filename>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="<filename>"

<encoded content after a blank line that divide the header and the body>
 
I found out that the function imap_savebody($mbox, $file, $emailid, "", 0) gives the complete email data in an eml format. That's what I was looking for.
Thank you all for your feedback.
 
To include attachments in .eml files, save the full MIME content of the email. Use imap_fetchheader() and imap_body() to fetch headers, body, and attachments together.
 
Back
Top