Python、IMAP 和 GMail.将消息标记为已查看

2022-01-23 00:00:00 python email imap gmail

问题描述

我有一个 python 脚本,它必须获取看不见的消息,对其进行处理,并将其标记为已见(或已读)

I have a python script that has to fetch unseen messages, process it, and mark as seen (or read)

我在登录后这样做:

    typ, data = self.server.imap_server.search(None, '(UNSEEN)')

    for num in data[0].split():
        print "Mensage " + str(num) + " mark"
        self.server.imap_server.store(num, '+FLAGS', '(SEEN)')

第一个问题是,搜索返回所有消息,而不仅仅是 UNSEEN.第二个问题是邮件没有标记为 SEEN.

The first problem is that, the search returns ALL messages, and not only the UNSEEN. The second problem is that messages are not marked as SEEN.

谁能帮帮我?

谢谢!


解决方案

import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com', '993')
obj.login('user', 'password')
obj.select('Inbox')   <--- it will select inbox
typ ,data = obj.search(None,'UnSeen')
obj.store(data[0].replace(' ',','),'+FLAGS','Seen')

相关文章