Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否使用IMAPClient搜索主题中的关键字?_Python_Search_Imaplib_Subject_Imapclient - Fatal编程技术网

Python 是否使用IMAPClient搜索主题中的关键字?

Python 是否使用IMAPClient搜索主题中的关键字?,python,search,imaplib,subject,imapclient,Python,Search,Imaplib,Subject,Imapclient,下面返回未看到的电子邮件及其各自的主题行。如何仅返回与关键字匹配的未看到的电子邮件主题行 如果您有任何帮助,我们将不胜感激,请提前致谢 import email from imapclient import IMAPClient HOST = 'somthin.com' USERNAME = 'email' PASSWORD = 'pwd' with IMAPClient(HOST) as server: server.login(USERNAME, PASSWORD) server.sel

下面返回未看到的电子邮件及其各自的主题行。如何仅返回与关键字匹配的未看到的电子邮件主题行

如果您有任何帮助,我们将不胜感激,请提前致谢

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"))

我用imap_工具解决了我的问题

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)