简体   繁体   中英

Save email as .eml file with python

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?

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM