【问题标题】:How to download imap email attachment with base64 content in python?如何在python中下载带有base64内容的imap电子邮件附件?
【发布时间】:2020-09-27 21:34:18
【问题描述】:

我正在努力下载一个特定的 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()

【问题讨论】:

    标签: python-3.x base64 imap email-attachments


    【解决方案1】:

    尝试使用高级库。

    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

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 2010-09-25
      • 2013-04-11
      • 2020-07-06
      • 2022-08-03
      • 1970-01-01
      相关资源
      最近更新 更多