如何从msg文件中提取附件

时间:2012-03-30 05:53:19

标签: python

如何使用python ...从msg文件中提取附件? 示例代码段将非常有用......!

2 个答案:

答案 0 :(得分:2)

查看email examples,特别是根据mimetype解压缩消息。

答案 1 :(得分:1)

imap_host = 'imap.gmail.com'
mail = imaplib.IMAP4_SSL(imap_host)
mail.login(user,passw)
mail.select("Inbox")
mail = get_unseen(mail)

result, data = mail.uid('search', None, 'UNSEEN')
uid_list = data[0].split()
print len(uid_list), 'Unseen emails.'

for i in range(len(uid_list)):
    email_uid = uid_list[i]
    res, dat = mail.uid('fetch', email_uid, '(RFC822)')
    raw_email = dat[0][1]
    msg = email.message_from_string(raw_email)

    print ''
    print 'New email:\n'
    print i,'UID:', email_uid, 'Sender:', email.utils.parseaddr(msg['From'])[0],email.utils.parseaddr(msg['From'])[1]      
    print 'Subjct:',msg['Subject']
    print 'Message: '
    print get_body(msg)
    attach_list = get_attach_list(msg)
    print len(attach_list),'Attachments:',attach_list
    get_attach(msg)

def get_body(msg):
    for part in msg.walk():
        content_type = part.get_content_type()
        if content_type == 'text/plain' or content_type =='text/html':
            payload = part.get_payload(decode=True)
            if payload:
                print payload
    return

def get_attach_list(msg):
   attach_list=[]
   for part in msg.walk():
      filename = part.get_filename()
      if filename:
         attach_list.append(filename)
   return attach_list

def get_attach(msg):
    for part in msg.walk():
        filename = part.get_filename()
        if filename:
            fp = open(filename,'wb')
            fp.write(part.get_payload(decode=True))
            fp.close()
    return  

所以,基本上你要做的就是遍历完整的邮件,查找附件,然后使用get_payload(),下载附件。