簡體   English   中英

如何在Poplib中僅搜索包含附件的電子郵件

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

我正在嘗試使用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```

POP3協議不支持搜索或任何其他功能,該功能可能使您可以確定哪些郵件帶有附件。 如果您被POP3卡住了,那您就不走運了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM