Python download attach of email

4 views
Skip to first unread message

Chris

unread,
Mar 3, 2013, 10:40:43 AM3/3/13
to qda_...@googlegroups.com

在此输入代码...import imaplib
import email
import os

imaplib.IMAP4.debug = imaplib.IMAP4_SSL.debug = 1

Pwd = 'xxxxxx'
Host = 'imap.163.com'

# Login mail
username, passwd = (Mail, Pwd)
con = imaplib.IMAP4_SSL(Host)
con.login(username, passwd)
con.select()
# Get all unseen mail
typ, data = con.search(None, '(UNSEEN)')


for num in data[0].split():
    #  retrieve the entire message
    typ, data = con.fetch(num, '(RFC822)')
    text = data[0][1]
    msg = email.message_from_string(text)
    for part in msg.walk():
        # multipart
        if part.get_content_maintype() == 'multipart':
            continue
        # No attached
        if part.get('Content-Disposition') is None:
            continue
        # Get attached file name
        filename = part.get_filename()
        data = part.get_payload(decode=True)
        if not data:
            continue
        # Download attached.
        f = open('C:\\' + filename, 'w')
        print 'C:\\', filename
        f.write(data)
        f.close()

con.close()
con.logout()
All:
Just download the unseen mail's attach. I will continue to add some other function.
add post it at this forum.


Reply all
Reply to author
Forward
0 new messages