如何在python中下载带有base64内容的imap电子邮件附件?



我正在努力下载一个特定的pdf文件(带水印(作为电子邮件附件?如果您给我您的电子邮件地址,我可以将其发送到您的电子邮件?

我尝试了以下一块:-

fp = open(mail.filePath, 'wb')
body = mail.part.get_payload(decode = True)
file_data = base64.encodestring(body).decode()
file_data = file_data.encode('UTF-8')
#file_data = base64.urlsafe_b64decode(mail.part.get_payload(decode=True).encode('UTF-8'))
fp.write(file_data)
fp.close()

尝试使用高级库。

from imap_tools import MailBox
# 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)

https://github.com/ikvk/imap_tools

最新更新