need to strip stuff off email

Tim Williams listserver at tdw.net
Wed Jun 22 05:48:19 EDT 2005



> "nephish" <nephish at xit.net> wrote in message
> news:mailman.723.1119399951.10512.python-list at python.org...
> > hey there,
> > i have a script that retrieves my email, but i need it to
> > be able to strip all the stuff off except the body (the message itself)
> > so i can later write it to a text file.
> >
> > anyone know how to accomplish this?
> > thanks

The body is:   The rest of the email after "the first blank line after the
subject header".   In practice it is the first blank line.

If you get the message into a string it can sometimes be easier to just
RSPLIT the string at '\r\n\r\n',  if the message is in a list then the body
= '\r\n'.join(msg[x:]) where x = that blank line +1  ,  that way if you
don't need any of the header info,  you don't have to decode the message and
rebuild it in a file.

if you *are* using the email module, eg

msg = email.message_from_file(a_file)

then rsplit the msg to get the same result.

As someone will no doubt point out,   some emails are broken and parts of
the headers will end up in the body (even when you view the email it in a
client),  this is very rare though.







More information about the Python-list mailing list