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

How to download imap email attachment with base64 content in python?

我很难下载一个特定的 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