【问题标题】:How to search for only emails containing attachments in poplib如何在poplib中仅搜索包含附件的电子邮件
【发布时间】:2019-09-02 03:13:19
【问题描述】:

我正在尝试使用 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```

【问题讨论】:

    标签: python python-2.7 email pop3


    【解决方案1】:

    POP3 协议不支持搜索或任何其他可能允许您确定哪些邮件有附件的功能。如果你被 POP3 卡住了,那你就不走运了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2020-07-07
      • 2011-04-10
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多