The lib email parse problem...

叮叮当当 guotie.9 at gmail.com
Wed Aug 30 22:41:58 EDT 2006


thanks.

i have use a temp method to overcome it .

i still think the email lib should give the boundary border to parse
mail.

code is as following:

    def parse_mail_content(self, mail):
        content =    ''
        alter =    False
        subty =    ''
        html =    ''
        plain =    ''
        for part in mail.walk():
            if part.is_multipart():
                if part.get_content_subtype() ==    'alternative':
                    alter =    True
                else:
                    alter =    False
                continue
            if part.get_content_maintype() ==    'text':
                if part.get_filename():
                    continue
                ty =    part.get_content_subtype()
                ch =    part.get_content_charset()
                if alter and ty ==    'plain':
                    subty =    'plain'
                    if ch:
                        plain =    unicode(part.get_payload(decode =
True),ch).encode('utf-8')
                    else:
                        plain =    part.get_payload(decode =
True).decode('gb2312').encode('utf-8')
                elif alter and ty ==    'html':
                    subty =    'html'
                    if ch:
                        html =    unicode(part.get_payload(decode =
True),ch).encode('utf-8')
                    else:
                        html =    part.get_payload(decode =
True).decode('gb2312').encode('utf-8')
                elif not alter:
                    if subty ==    'html':
                        content +=    html
                    elif subty ==    'plain':
                        content +=    plain
                    alter =    False
                    subty =    ''
                    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')
            elif alter:
                if subty ==    'html':
                    content +=    html
                elif subty ==    'plain':
                    content +=    plain
                alter =    False
                subty =    ''
        if alter:
            if subty ==    'html':
                content +=    html
            elif subty ==    'plain':
                content +=    plain
        return content

thanks very much.

John Machin wrote:
> On 30/08/2006 4:44 PM, 叮叮当当 wrote:
> > yes, the special is i must choose exactly one section to destruct,
> > instead of processing all subparts.
>
> So have you tried to use the example I posted yesterday? Do you still
> have any problems? Note: it is generally a good idea to post a message
> when you have overcome a problem -- that lets would-be helpers know that
> they are "off the case" :-)
> 
> Cheers,
> John




More information about the Python-list mailing list