0

I am trying to export an Email message obtained with aiosmtpd to an *.eml file that I can later open with any *.eml viewer or email client like Thunderbird or Mail.

    async def handle_DATA(self, server, session, envelope) -> str:
        msg: EmailMessage = message_from_bytes(envelope.content, policy=policy.SMTPUTF8)

        with open("test.eml", 'wb') as outfile:
            outfile.write(msg.as_bytes())

I've also tried to save the file with Generator with both unixfrom=False and unixfrom=False and same thing.

        with open("test.eml", 'w') as file:
            emlGenerator = generator.Generator(file)
            emlGenerator.flatten(msg, unixfrom=False)

The file gets created correctly but not all the eml files can be read correctly by Mail or Thunderbird.

Emails received from gmail.com are created correctly but emails received from protonmail.com are not. Eml files created from Protonmail emails can be opened but only from and to parameters of the email can be seen. I can't see the content neither the attachments in it.

I guess it does not have the format that eml parsers are expecting to see.

I've tried with different policies (like policy.SMTP, policy.default, ...).

What is the proper way to create eml files with python?

NOTE: The content of the EML file for Protonmail contains the following This is an OpenPGP/MIME signed message (RFC 4880 and 3156). Could this be related with the lack of correct parsing for Protonmail emails?

user1618465
  • 1,813
  • 2
  • 32
  • 58
  • You write that the eml files cannot be read correctly by Mail or Thunderbird. What happens if you send the email directly to Mail/Thunderbird, instead of going through aiosmtpd/eml? Can they be read correctly in that case? – Mathias Rav Aug 01 '22 at 16:41
  • Can you show an actual example of something that Thunderbird cannot open? – tripleee Oct 06 '22 at 07:19

2 Answers2

0

NOTE: The content of the EML file for Protonmail contains the following This is an OpenPGP/MIME signed message (RFC 4880 and 3156). Could this be related with the lack of correct parsing for Protonmail emails?

Yes. Protonmail encrypts emails stored on their servers using PGP. So in order to view those encrypted emails opened in Thunderbird, the user would need to follow Protonmail's directions to download their public and private PGP keys, and then follow these directions to install those inside Thunderbird. For Apple Mail, it appears that users would need to install GPGTools.

Another option would be to roll your own PGP implementation using Python, to decrypt the text and attachments of the Protonmail emails as your app downloads and converts them to .eml. Here is a SO answer discussing one approach to that. However, that would only work if these emails are coming SOLELY from a Protonmail account that you (the developer) control. Otherwise, your users would have to send you their PGP keys -- which for obvious security reasons is not a viable solution.

wopr_xl
  • 150
  • 1
  • 9
-1

I had the same issue with the Email API. Although it can not always be opened directly as an .eml file, it is correctly opened if the message is sent to an SMTP server. I understand the Email API generates a string that represents the data sent to the SMTP server. That data is to be interpreted by an SMTP server, not a client. The SMTP server will interpret this data and store it as if finds more convenient (i.e. storing it in a db).

A client like Thunderbird will retrieve the message using POP3 or IMAP, but that doesn't mean that it is retrieving the same message that was sent to the SMTP server, since the server might store it in a different format.

  • I can't help but think you are talking about something else than what the question is asking about. It's also not really an answer to the question, as far as I can tell. – tripleee Oct 06 '22 at 07:19
  • He's trying to save an .eml message using the Email API to open it from a mail client. The resulting file is not open properly in an email client because it is intended to be sent to an SMTP server. The Email API is not intended for generating an .eml message that can be opened by a mail client. – Xavi Conde Oct 07 '22 at 12:02
  • It's not clear which Email API you are talking about. The RFC5322 message created by `EmailMessage.as_bytes()` is exactly the format that RFC-compliant email clients can read from disk. – tripleee Oct 07 '22 at 12:04
  • After doing some testing, I've noticed that the .eml file had LF as end of line character. When I open the file in Outlook, the content is not correct. After replacing the LF by CR+LF, Outlook opens it correctly. Also, when GMail donwloads the mail as .eml, CR+LF is used as end of line. – Xavi Conde Oct 07 '22 at 15:00
  • That sounds like an important hint, but we'd have to wait for the OP to confirm. Unfortunately, they no longer seem to be responding. – tripleee Oct 07 '22 at 19:08