简体   繁体   English

Python IMAP 下载所有附件

[英]Python IMAP download all attachments

I need to iterate over all the mail into a GMAIL inbox.我需要将所有邮件迭代到 GMAIL 收件箱中。 Also I need to download all the attachments for each mail (some mails have 4-5 attachments).另外我需要下载每封邮件的所有附件(有些邮件有 4-5 个附件)。 I found some helps here : https://stackoverflow.com/a/27556667/8996442我在这里找到了一些帮助: 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

But, it download only one attachment per e-mail (but the author of the post mention that norammly it download all, no?).但是,它每封电子邮件只下载一个附件(但帖子的作者提到它通常会下载所有附件,不是吗?)。 The print(filename) show me only one attachment Any idea why ? print(filename)只显示一个附件知道为什么吗?

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

https://pypi.org/project/imap-tools/ https://pypi.org/project/imap-tools/

As already pointed out in comments, the immediate problem is that return exits the for loop and leaves the function, and you do this immediately when you have saved the first attachment.正如评论中已经指出的那样,当前的问题是return退出for循环并离开函数,并且在保存第一个附件后立即执行此操作。

Depending on what exactly you want to accomplish, change your code so you only return when you have finished all iterations of msg.walk() .根据您究竟想要完成什么,更改您的代码,以便您仅在完成msg.walk()所有迭代后才return Here is one attempt which returns a list of attachment filenames:这是返回附件文件名列表的一种尝试:

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

See the inline comments for explanations of what I changed and why.请参阅内嵌注释以了解我更改的内容和原因。

In general, avoid print() ing stuff from inside a worker function;一般来说,避免在工作函数内部使用print()东西; either use logging to print diagnostics in a way that the caller can control, or just return the information and let the caller decide whether or not to present it to the user.要么使用logging以调用者可以控制的方式打印诊断信息,要么只返回信息并让调用者决定是否将其呈现给用户。

Not all MIME parts have a Content-Disposition: ;并非所有 MIME 部分都有Content-Disposition: ; in fact, I would expect this to miss the majority of attachments, and possibly extract some inline parts.事实上,我希望这会错过大多数附件,并可能提取一些内联部分。 A better approach is probably to look whether the part has Content-Disposition: attachment and otherwise proceed to extract if either there is no Content-Disposition: or the Content-Type: is not either text/plain or text/html .更好的方法可能是查看该部分是否有Content-Disposition: attachment ,如果没有Content-Disposition:Content-Type:不是text/plaintext/html ,则继续提取。 Perhaps see also What are the "parts" in a multipart email?也许另请参阅多部分电子邮件中的“部分”是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM