How can I get text of the body (payload) of an email?

Jeffrey Froman jeffrey at fro.man
Sat Oct 16 17:06:53 CEST 2004


andrew blah wrote:

> I want to get the text of the body - every character from the new line
> after the headers until the end of the message.
> 
> My objective is to do an SHA hash on the body text so the get_payload
> method isn't what I am after.

Funny, I recently undertook the same task. Here's my solution:

msg = email.message_from_string(foo)
x = sha.new()
for line in email.Iterators.body_line_iterator(msg):
    x.update(line)
hash = x.digest()

This very cool iterator returns every body line, but skips all the headers,
including the headers present in each sub-part of the email. If you only
want plain text parts, you might combine this iterator with
email.Iterators.typed_subpart_iterator().

Jeffrey



More information about the Python-list mailing list