Beispiel #1
0
def parse_bodystructure(data):
    '''parses data from any of the following:
      FETCH BODY
      FETCH BODYSTRUCTURE
      return a list tuples (partnumber, description)
    '''
    return bodystructure.parse_bodystructure(data)
Beispiel #2
0
def read_mail_api(box, mailid):
    print i.select(box)
    #mail = i.fetch(mailid, '(BODY.PEEK[HEADER])')
    body_structure = i.fetch(mailid, 'BODYSTRUCTURE')
    print body_structure
    header = fetch_header(mailid)
    parsed_header = email.message_from_string(header)
    # decode header
    clean_parsed_header = getheader_from_dict(parsed_header)
    print clean_parsed_header
    html_body = ""
    text_body = ""
    parsed_bodystructure = parse_bodystructure(body_structure[1][0])
    # if mail is only one part, there is no section number
    if len(parsed_bodystructure) == 1:
        p = parsed_bodystructure[0].split()
        if p[0] == '"text"':
            if p[1] == '"plain"':
                _m = i.fetch(mailid, '(body[])')[1][0][1]
                _m = email.message_from_string(_m)
                print _m
                text_body = unicode(
                        _m.get_payload(),
                        _m.get_charset() or _m.get_content_charset() or 'ascii',
                        'replace')
            elif p[1] == '"html"':
                _m = i.fetch(mailid, '(body[])')[1][0][1]
                _m = email.message_from_string(_m)
                html_body = unicode(
                        _m.get_payload(),
                        _m.get_charset() or _m.get_content_charset() or 'ascii',
                        'replace')
    else:
        for p in parsed_bodystructure:
            p = p.split()
            if len(p) > 1:
                if p[1] == '"text"':
                    if p[2] == '"plain"':
                        text_body += get_email_body(mailid, p[0])
                    elif p[2] == '"html"':
                        html_body += get_email_body(mailid, p[0])
    #print html_body
    #print text_body
    return {'headers': clean_parsed_header,
            'plaintext': text_body,
            'html': html_body,
            'attachements': None}