gpt4 book ai didi

Python - imaplib - 查看发送给特定发件人的消息

转载 作者:太空狗 更新时间:2023-10-29 21:53:25 25 4
gpt4 key购买 nike

我有一个 python 脚本,用于检查 Gmail 的 IMAP 服务器上的电子邮件,它显示服务器上的最新电子邮件。但是,该电子邮件地址正在接收来自多个不同帐户的电子邮件。我该怎么做才能使用此脚本并让它只查看发给特定发件人的邮件?例如,任何只发送到“johndoe@gmail.com”的电子邮件都会出现。

import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myemail@gmail.com', 'password123')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.

result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID

raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

提前致谢! :)

最佳答案

改变:

result, data = mail.search(None, "ALL")

到:

result, data = mail.search(None, '(TO "johndoe@gmail.com")')

关于Python - imaplib - 查看发送给特定发件人的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12944727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com