我无法从邮箱中发送的邮件中获取附件到邮箱

I can't get the attachment from a mail sent in the mail box to the mail box

我制作了一个 Python 程序,可以将附件发送到邮箱,该邮箱是我项目的主邮箱。邮件是从邮箱本身发送到邮箱本身,因此对于收件人和发件人来说是同一个地址。 (见图片)

这是我的代码,适用于不是来自同一邮箱的附件。我只是无法获得附件。再做一个邮箱很烦人

import os
from imbox import Imbox
import traceback

host = "imap.gmail.com"
download_folder = "G:\DownloadFMail"
username = "example@gmail.com"
password = "123456"

mail = Imbox(host, username, password, ssl=True, ssl_context=None, starttls=False)
mailAttach = mail.messages(unread=True,sent_from = username)

for (uid, message) in mailAttach:
    mail.mark_seen(uid) # optional, mark message as read

    for idx, attachment in enumerate(message.attachments):
        try:
            att_fn = attachment.get('filename')
            download_path = f"{download_folder}/{att_fn}"
            print(download_path)
            with open(download_path, "wb") as fp:
                fp.write(attachment.get('content').read())
        except:
            print(traceback.print_exc())

mail.logout()

已解决此问题,但问题出在我发送附件的方式上。我在这里使用 stmplib 发送它,但是在邮件详细信息中没有任何 header 附件:

这就是为什么它没有被 Imbox 检测为附件的原因。 现在它看起来像这样并且工作完美:

这是代码示例:

with open(zipfile, "rb") as attachment:
     payload = MIMEBase('application', 'octate-stream')
     payload.set_payload((attachment).read())
     encoders.encode_base64(payload) #encode the attachment
     payload.add_header('Content-Disposition','attachment',filename="image.zip")
     message.attach(payload) here

如果不够清楚or/and需要进一步解释的人问我。