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?