Reading EmailMessage from file

Paul Moore p.f.moore at gmail.com
Mon Jul 16 05:40:31 EDT 2018


On 16 July 2018 at 02:31, Skip Montanaro <skip.montanaro at gmail.com> wrote:
>> What are you actually trying to do? You're talking like you're trying
>> to read an existing RFC822 email-with-headers from a file, but you're
>> showing code that creates a new email with body content set from
>> a file, which is a completely different thing.
>
> Yes, that's exactly what I'm trying to do. A bit more context... I'm
> trying to port SpamBayes from Python 2 to Python 3. The file I
> attached which failed to come through was exactly what you suggested,
> an email in a file. That is what the example from the 3.7 docs
> suggested I should be able to do. Had the message in the file been
> encoded as utf-8, that would have worked. I just tested it with
> another message which is utf-8-encoded.

As I understand it, an email is a logical object, and when it's saved
to disk it should be done in some mailbox format or other. So rather
than grabbing raw file data and trying to deserialise it, maybe you'd
be better using one of the classes in the mailbox module
(https://docs.python.org/3.7/library/mailbox.html)?

Alternatively, maybe email.parser.message_from_binary_file (with a
file object opened in binary) would do what you're after?

Note: I've not actually used any of these methods myself...

Paul



More information about the Python-list mailing list