使用Python解析Gmail并将所有早于日期的内容标记为“已读”
长话短说,我创建了一个新的 Gmail 帐户,并将其他几个帐户链接到它(每个帐户都有 1000 条消息),我正在导入这些帐户。所有导入的消息均以未读状态到达,但我需要它们显示为已读。
我对 python 有一点经验,但我只使用 mail 和 imaplib 模块来发送邮件,而不是处理帐户。
有没有办法批量处理收件箱中的所有项目,并将早于指定日期的邮件标记为已读?
Long story short, I created a new gmail account, and linked several other accounts to it (each with 1000s of messages), which I am importing. All imported messages arrive as unread, but I need them to appear as read.
I have a little experience with python, but I've only used mail and imaplib modules for sending mail, not processing accounts.
Is there a way to bulk process all items in an inbox, and simply mark messages older than a specified date as read?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是对 store 方法的 imaplib 文档页面 中的代码的轻微修改。我从 RFC 3501 找到了要使用的搜索条件。这应该可以帮助您开始。
This is a slight modification of the code in the imaplib doc page for the store method. I found the search criteria to use from RFC 3501. This should get you started.
基于 Philip T. 的上述回答以及 RFC 3501 和 RFC 2822,我构建了一些代码行将超过 10 天的邮件标记为已读。静态列表用于缩写月份名称。这不是特别优雅,但 Python 的 %b 格式字符串与语言环境相关,这可能会带来令人不快的意外。所有 IMAP 命令都是基于 UID 的。
顺便说一句:我不知道,为什么在我的例子(dovecot IMAP 服务器)中必须使用“-”作为搜索字符串中的日期分隔符。对我来说,这似乎与 RFC 2822 相矛盾。但是,以简单空格作为分隔符的日期仅返回 IMAP 错误。
Based on Philip T.'s answer above and RFC 3501 and RFC 2822, I built some lines of code to mark mails older than 10 days as read. A static list is used for the abbreviated month names. This is not particularly elegant, but Python's %b format string is locale dependent, which could give unpleasant surprises. All IMAP commands are UID based.
By the way: I do not know, why "-" had to be used as a date delimiter in the search string in my case (dovecot IMAP server). To me that seems to contradict RFC 2822. However, dates with simple whitespace as delimiter only returned IMAP errors.
与其尝试解析 HTML,为什么不直接使用 IMAP 接口呢?将其连接到标准邮件客户端,然后按日期排序并标记您想要阅读的内容。
Rather than try to parse our HTML why not just use the IMAP interface? Hook it up to a standard mail client and then just sort by date and mark whichever ones you want as read.
只需转到 Gmail 网络界面,按日期进行高级搜索,然后选择全部并标记为已读。
Just go to the Gmail web interface, do an advanced search by date, then select all and mark as read.