使用Python解析Gmail并将所有早于日期的内容标记为“已读”

发布于 2024-08-02 09:48:24 字数 205 浏览 5 评论 0原文

长话短说,我创建了一个新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箹锭⒈辈孓 2024-08-09 09:48:24
typ, data = M.search(None, '(BEFORE 01-Jan-2009)')
for num in data[0].split():
   M.store(num, '+FLAGS', '\\Seen')

这是对 store 方法的 imaplib 文档页面 中的代码的轻微修改。我从 RFC 3501 找到了要使用的搜索条件。这应该可以帮助您开始。

typ, data = M.search(None, '(BEFORE 01-Jan-2009)')
for num in data[0].split():
   M.store(num, '+FLAGS', '\\Seen')

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.

寂寞陪衬 2024-08-09 09:48:24

基于 Philip T. 的上述回答以及 RFC 3501RFC 2822,我构建了一些代码行将超过 10 天的邮件标记为已读。静态列表用于缩写月份名称。这不是特别优雅,但 Python 的 %b 格式字符串与语言环境相关,这可能会带来令人不快的意外。所有 IMAP 命令都是基于 UID 的。

import imaplib, datetime

myAccount = imaplib.IMAP4(<imapserver>)
myAccount.login(<imapuser>, <password>)
myAccount.select(<mailbox>)

monthListRfc2822 = ['0', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
beforeDate = datetime.datetime.today() - datetime.timedelta(days = 10)
beforeDateString = ("(BEFORE %s-%s-%s)"
                    % (beforeDate.strftime('%d'),
                       monthListRfc2822[beforeDate.month],
                       beforeDate.strftime('%Y')))
typ, data = myAccount.uid('SEARCH', beforeDateString)
for uid in data[0].split():
    myAccount.uid('STORE', uid, '+FLAGS', '(\Seen)')

顺便说一句:我不知道,为什么在我的例子(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.

import imaplib, datetime

myAccount = imaplib.IMAP4(<imapserver>)
myAccount.login(<imapuser>, <password>)
myAccount.select(<mailbox>)

monthListRfc2822 = ['0', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
beforeDate = datetime.datetime.today() - datetime.timedelta(days = 10)
beforeDateString = ("(BEFORE %s-%s-%s)"
                    % (beforeDate.strftime('%d'),
                       monthListRfc2822[beforeDate.month],
                       beforeDate.strftime('%Y')))
typ, data = myAccount.uid('SEARCH', beforeDateString)
for uid in data[0].split():
    myAccount.uid('STORE', uid, '+FLAGS', '(\Seen)')

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.

你爱我像她 2024-08-09 09:48:24

与其尝试解析 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.

一个人的旅程 2024-08-09 09:48:24

只需转到 Gmail 网络界面,按日期进行高级搜索,然后选择全部并标记为已读。

Just go to the Gmail web interface, do an advanced search by date, then select all and mark as read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文