Reading EmailMessage from file

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


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

https://docs.python.org/3/library/email.examples.html

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(fp.read())

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.

Thx,

Skip


More information about the Python-list mailing list