The lib email parse problem...

叮叮当当 guotie.9 at gmail.com
Tue Aug 29 05:39:13 EDT 2006


this is just a temp solution for the simplest email format as my
example, and i cannot always only show the html part.

but in fact , there are many more difficult mail format

btw, i know how to use walk(), and the question is not this.

my code is as the following:

def mail_content(mail):
    content =    ''
    for part in mail.walk():
        if part.is_multipart():
            continue

        ch =    part.get_content_charset()
        if ch:
            content +=    unicode(part.get_payload(decode =
True),ch).encode('utf-8')
        else:
            content +=    part.get_payload(decode =
True).decode('gb2312').encode('utf-8')
    return content

Fredrik Lundh 写道:

> "????" wrote:
>
> > the plain text is abcd, and the alternative content type is text/html,
> > i should prefer explain the html content, and i must not explaint the
> > two part ,so i want to get the boundary end.
>
> so use the email module:
>
>     import email
>
>     message_text = "..."
>
>     message = email.message_from_string(message_text)
>
>     for part in message.walk():
>         if part.get_content_type() == "text/html":
>             print "html is", repr(part.get_payload())
>
> (the message instances either contains a payload or sequence of submessages;
> use message.is_multipart() to see if it's a sequence or not.  the walk() method
> used in this example loops over all submessages, in message order).
> 
> </F>




More information about the Python-list mailing list