0

下面返回看不见的电子邮件及其各自的主题行。如何只返回与关键字匹配的看不见的电子邮件主题行?

任何帮助将不胜感激,非常感谢提前....

import email
from imapclient import IMAPClient

HOST = 'somthin.com'
USERNAME = 'email'
PASSWORD = 'pwd'

with IMAPClient(HOST) as server:
server.login(USERNAME, PASSWORD)
server.select_folder("INBOX", readonly=True)

messages = server.search("UNSEEN")
for uid, message_data in server.fetch(messages, "RFC822").items():
    email_message = email.message_from_bytes(message_data[b"RFC822"])
    print(uid, email_message.get("Subject"))
4

1 回答 1

1

我使用 imap_tools 来解决我的问题...

from imap_tools import MailBox, AND, OR, NOT
from pprint import pprint    

# get list of email subjects from INBOX folder
with MailBox('xyz.com').login('email', 'pwd', initial_folder='INBOX') as mailbox:
    subjects = [msg.subject for msg in mailbox.fetch(AND(subject='keyword'))]
    pprint(subjects)
于 2021-03-20T19:22:04.223 回答