python imaplib-将电子邮件标记为未读或未读 [英] python imaplib - mark email as unread or unseen

查看:572
本文介绍了python imaplib-将电子邮件标记为未读或未读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在imap中自动将邮件标记为SEEN,也可以在此处和互联网上进行搜索.

Searching here and on the internet, there are a lot of examples to how to mark a message as SEEN, even though this is automatic with imap.

但是如何将电子邮件标记为UNSEENUNREAD.

But how can I mark an email as UNSEEN or UNREAD.

我在python中有一个脚本,可以接收UNSEEN消息,并且效果很好.但是,在阅读它们之后,imap会自动将它们标记为SEEN,但仅在脚本没有错误时才可以正常工作,因为如果引发异常,我希望将电子邮件再次标记为UNSEEN,因此下次使用该脚本将会再次阅读该消息.

I have a script in python which receives UNSEEN messages, and it works great. But after reading them, imap automatically marks them as SEEN which works fine but only if the script has no errors, because if it raises an exception, I want the email to be marked again as UNSEEN, so next time the script will read that message again.

我怎么能做到这一点?

我也使用过mail.select(mail_label,readonly=True),但是这并没有帮助,因为我不能将消息标记为我也需要的SEEN.我也希望它可以与Gmail一起使用.

I have also used mail.select(mail_label,readonly=True), but it doesn't help because with that I cannot mark a message as SEEN which I also need. I also want this to work with Gmail.

推荐答案

您可以使用以下命令轻松清除\Seen标志:

You can easily clear the \Seen flags with this command:

tag UID STORE -FLAGS (\Seen)

,但是,如果仅在成功处理消息后仅将\Seen标志设置为第一位,则您的软件可能会更强大.这样,如果在处理消息时发生任何错误(即使与IMAP服务器的连接断开),该标志也会保持未设置状态,您可以在下一次脚本运行时重试该消息.您可以通过使用BODY.PEEK而不是BODY来避免IMAP服务器自动设置\Seen标志的方法.

but your software will probably be more robost if you only set the \Seen flag in the first place after you have successfully processed a message. That way, if anything goes wrong while you are processing a message (even if the connection to the IMAP server is broken) the flag remains unset and you can retry that message the next time the script runs. You do this by avoiding the IMAP server's automatic setting of the \Seen flag by using BODY.PEEK instead of BODY.

在Python中,我认为应该这样发出STORE命令,但是我没有尝试过.

In Python, I think that STORE command should be issued like this but I haven't tried it.

connection.uid('STORE', '-FLAGS', '(\Seen)')

这篇关于python imaplib-将电子邮件标记为未读或未读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆