Parsing Email Headers

Thomas Guettler hv at tbz-pariv.de
Fri Mar 12 07:59:54 EST 2010


T wrote:
> Thanks for your suggestions!  Here's what seems to be working - it's
> basically the same thing I originally had, but first checks to see if
> the line is blank
> 
>                 response, lines, bytes = M.retr(i+1)
>                 # For each line in message
>                 for line in lines:
>                     if not line.strip():
>                         M.dele(i+1)
>                         break
> 
>                     emailMessage = email.message_from_string(line)
>                     # Get fields
>                     fields = emailMessage.keys()
>                     # If email contains "From" field
>                     if emailMessage.has_key("From"):
>                         # Get contents of From field
>                         from_field = emailMessage.__getitem__("From")

Hi T,

wait, this code looks strange.

You delete the email if it contains an empty line? I use something like this:

message='\n'.join(connection.retr(msg_num)[1])

Your code:
emailMessage = email.message_from_string(line)
create an email object from only *one* line!

You retrieve the whole message (you don't save bandwith), but maybe that's
what you want.


  Thomas

-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de



More information about the Python-list mailing list