[英]Python IMAP download all attachments
我需要将所有邮件迭代到 GMAIL 收件箱中。 另外我需要下载每封邮件的所有附件(有些邮件有 4-5 个附件)。 我在这里找到了一些帮助: https : //stackoverflow.com/a/27556667/8996442
def save_attachments(self, msg, download_folder="/tmp"):
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
print(filename)
att_path = os.path.join(download_folder, filename)
if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
return att_path
但是,它每封电子邮件只下载一个附件(但帖子的作者提到它通常会下载所有附件,不是吗?)。 print(filename)
只显示一个附件知道为什么吗?
从 imap_tools 导入邮箱
# get all attachments from INBOX and save them to files
with MailBox('imap.my.ru').login('acc', 'pwd', 'INBOX') as mailbox:
for msg in mailbox.fetch():
for att in msg.attachments:
print(att.filename, att.content_type)
with open('C:/1/{}'.format(att.filename), 'wb') as f:
f.write(att.payload)
正如评论中已经指出的那样,当前的问题是return
退出for
循环并离开函数,并且在保存第一个附件后立即执行此操作。
根据您究竟想要完成什么,更改您的代码,以便您仅在完成msg.walk()
所有迭代后才return
。 这是返回附件文件名列表的一种尝试:
def save_attachments(self, msg, download_folder="/tmp"):
att_paths = []
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
# Don't print
# print(filename)
att_path = os.path.join(download_folder, filename)
if not os.path.isfile(att_path):
# Use a context manager for robustness
with open(att_path, 'wb') as fp:
fp.write(part.get_payload(decode=True))
# Then you don't need to explicitly close
# fp.close()
# Append this one to the list we are collecting
att_paths.append(att_path)
# We are done looping and have processed all attachments now
# Return the list of file names
return att_paths
请参阅内嵌注释以了解我更改的内容和原因。
一般来说,避免在工作函数内部使用print()
东西; 要么使用logging
以调用者可以控制的方式打印诊断信息,要么只返回信息并让调用者决定是否将其呈现给用户。
并非所有 MIME 部分都有Content-Disposition:
; 事实上,我希望这会错过大多数附件,并可能提取一些内联部分。 更好的方法可能是查看该部分是否有Content-Disposition: attachment
,如果没有Content-Disposition:
或Content-Type:
不是text/plain
或text/html
,则继续提取。 也许另请参阅多部分电子邮件中的“部分”是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.