我正在尝试获取来自Gmail帐户的所有消息,这些消息可能包含一些大附件(约30MB)。 我只需要名称,而不是整个文件。 我找到了一段代码来获取消息和附件的名称,但它会下载文件,然后读取其名称:
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库都可以满足我的需求。此致敬礼。