简体   繁体   English

如何在Poplib中仅搜索包含附件的电子邮件

[英]How to search for only emails containing attachments in poplib

I am trying to use poplib to search through emails and get only the ones with attachments, I have some current code but it's so slow as it downloads all the email messages, is there any way to just search the server for emails that have attachments and then download them? 我正在尝试使用poplib搜索电子邮件并仅获取带有附件的电子邮件,我有一些当前代码,但是它很慢,因为它下载了所有电子邮件,是否有任何方法可以仅在服务器上搜索包含附件和然后下载它们?

def fetch_mail(delete_after=False):
    pop_conn = mail_connection()
    print("connected")
    messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
    messages = ["\n".join(mssg[1]) for mssg in messages]
    messages = [parser.Parser().parsestr(mssg) for mssg in messages]
    if delete_after == True:
        delete_messages = [pop_conn.dele(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
    pop_conn.quit()
    return messages


allowed_mimetypes = ["text/plain"]


def get_attachments():
    messages = fetch_mail()
    attachments = []
    for msg in messages:
        for part in msg.walk():
            if part.get_content_type() in allowed_mimetypes:
                name = part.get_filename()
                data = part.get_payload(decode=True)
                if name != None:
                    ranint = random.randint(100000,999999)
                    f = open(str(ranint) + name, 'wb')
                    f.write(data)
                    f.close()
                    attachments.append(name)
            else:
                continue
    return attachments```

The POP3 protocol does not support searching or any other feature which might allow you to determine which messages have attachments. POP3协议不支持搜索或任何其他功能,该功能可能使您可以确定哪些邮件带有附件。 If you are stuck with POP3, you are out of luck. 如果您被POP3卡住了,那您就不走运了。

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

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