我試圖從一個可能包含一些大附件(約30MB)的Gmail帳戶獲取所有郵件。我只需要名稱,而不是整個文件。我發現了一段代碼得到一個消息和附件的名字,但它下載的文件,然後讀取它的名字:獲取Gmail附件文件名而不下載它
import imaplib, email
#log in and select the inbox
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('username', 'password')
mail.select('inbox')
#get uids of all messages
result, data = mail.uid('search', None, 'ALL')
uids = data[0].split()
#read the lastest message
result, data = mail.uid('fetch', uids[-1], '(RFC822)')
m = email.message_from_string(data[0][1])
if m.get_content_maintype() == 'multipart': #multipart messages only
for part in m.walk():
#find the attachment part
if part.get_content_maintype() == 'multipart': continue
if part.get('Content-Disposition') is None: continue
#save the attachment in the program directory
filename = part.get_filename()
fp = open(filename, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print '%s saved!' % filename
我必須這樣做一分鐘一次,所以我不能下載數MB數據。我是網頁腳本的新手,所以任何人都可以幫助我?我實際上並不需要使用imaplib,任何python lib都可以。
問候
你可以發送只有20MB的Gmail你知道嗎? – 0x90
我的意思是所有消息中的所有附件。 – mopsiok