使用 IMAP4 和 python 从 GMail 获取加星标的邮件

发布于 2024-10-18 20:13:20 字数 156 浏览 5 评论 0原文

我发现了许多有关使用 IMAP 的虚拟信息,但我不明白如何将其用于我的目的。我找到了如何从邮箱中获取所有邮件和所有已看到的邮件,但是我应该如何使用星星? 请给我一个 python 代码示例,用于通过 IMAP4 从 GMail 获取加星标的邮件、检查某条邮件是否加星标、为某条邮件加星标和取消星标。

I found many dummy info about working with IMAP, but I didn't understand how to use it for my purposes. I found how I can get ALL messages from mailbox and ALL SEEN messages, but how should I work with stars?
Please, give me examples of python code for getting starred messages from GMail through IMAP4, for checking if some message is starred or unstarred, for starring and unstarring some one message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

ま昔日黯然 2024-10-25 20:13:20

Gmail 的“已加星标”状态直接映射到 IMAP \Flagged 关键字。因此,您可以通过在邮件上设置或取消设置 \Flagged 来切换邮件的星标:

IMAP4.store(num, '+FLAGS', '\\Flagged')

您可以通过搜索 FLAGGED 来搜索已加星标的邮件(或通过 搜索未加星标的邮件) UNFLAGGED):

IMAP4.search(None, 'FLAGGED')

Gmail 甚至为您提供一个包含所有加星标邮件的虚拟文件夹。如果您选择“[Gmail]/已加星标”,您将看到邮箱中所有已加星标的邮件:

IMAP4.select('[Gmail]/Starred')

Gmail's "Starred" state maps directly onto the IMAP \Flagged keyword. So you can toggle a message's star by setting or unsetting \Flagged on the message:

IMAP4.store(num, '+FLAGS', '\\Flagged')

You can search for starred messages by searching for FLAGGED (or for unstarred messages via UNFLAGGED):

IMAP4.search(None, 'FLAGGED')

Gmail even gives you a virtual folder containing all starred messages. If you SELECT "[Gmail]/Starred", you'll get a view of all the starred messages in the mailbox:

IMAP4.select('[Gmail]/Starred')
无声静候 2024-10-25 20:13:20
from imap_tools import MailBox, AND

with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:

    # get list of subjects of flagged emails from INBOX folder
    subjects = [msg.subject for msg in mailbox.fetch(A(flagged=True))]

    # set flag on emails from INBOX that html contains <b> tag
    flags = [imap_tools.MailMessageFlags.FLAGGED]
    mailbox.flag([m.uid for m in mailbox.fetch() if '<b>' in m.html], flags, True)

    # print flags for all emails from INBOX  
    for msg in mailbox.fetch(): print(msg.date, msg.flags)

我的外部库: https://github.com/ikvk/imap_tools

from imap_tools import MailBox, AND

with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:

    # get list of subjects of flagged emails from INBOX folder
    subjects = [msg.subject for msg in mailbox.fetch(A(flagged=True))]

    # set flag on emails from INBOX that html contains <b> tag
    flags = [imap_tools.MailMessageFlags.FLAGGED]
    mailbox.flag([m.uid for m in mailbox.fetch() if '<b>' in m.html], flags, True)

    # print flags for all emails from INBOX  
    for msg in mailbox.fetch(): print(msg.date, msg.flags)

My external lib: https://github.com/ikvk/imap_tools

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