Reading EmailMessage from file

Skip Montanaro skip.montanaro at
Sun Jul 15 18:16:06 EDT 2018

I have an email message in a file (see attached). According to this page:

something like this should construct an email message from the file:

>>> from email.message import EmailMessage
>>> msg = EmailMessage()
>>> fp = open("/home/skip/tmp/79487694")
>>> msg.set_content(

However, my default encoding is utf-8 and the mail message is encoded
using iso-8859-1, so it barfs trying to read the file.

I know I can explicitly specify the encoding in an isolated case like
this, but it seems like that shouldn't be necessary when reading a
message from a stream or file. I tried opening the file with "rb", but
got further downstream errors because I failed to set maintype and
subtype. Again, these are elements of the message structure and should
be decipherable by some bit of code which wants to construct the
message object. I'm clearly missing something.



More information about the Python-list mailing list