如何使用 python imap-tools

问题描述

from imap_tools import MailBox,AND
import re

yahooSmtpServer = "imap.mail.yahoo.com"

client = MailBox(yahooSmtpServer).login('myEmail','myPassword','INBox')
for msg in client.fetch(AND(seen=False)):
    mail = msg.html
    print(mail)
            

我不想在邮件出现在我的收件箱中后立即收到邮件。循环遍历这段代码,我总是可以检查未看到的消息,但这真的很麻烦,而且我不知道如何将消息标记为已读。

那么有什么方法可以使用 IMAP 工具在我的雅虎邮件收件箱中收到看不见的邮件?如果没有......我可以使用另一个图书馆吗? 谢谢。

解决方法

来自 imaptools documentation 和这个例子:

# SEEN: flag as unseen all messages sent at 05.03.2007 in current folder,*in bulk
mailbox.seen(mailbox.fetch("SENTON 05-Mar-2007"),False)

看来这段代码应该可以工作:

client = MailBox(yahooSmtpServer).login('myEmail','myPassword','INBOX')
for msg in client.fetch(AND(seen=False)):
    mail = msg.html
    print(mail)
# pass the email uid and bool here
    client.seen(msg.uid,True)
,

imap_tools BaseMailBox.fetch 有 mark_seen 参数。

默认情况下为 True,因此,默认情况下,电子邮件在获取时标记为“已看到”。

但您可以手动完成:

from imap_tools import MailBox
with MailBox('imap.mail.com').login('test@mail.com','pwd') as mailbox:
    uids = [msg.uid for msg in mailbox.fetch(mark_seen=False)]
    mailbox.seen(uids,True)

*IMAP 也有新的搜索条件