如何在 Python 中执行 IMAP 搜索(使用 Gmail 和 imaplib)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/179026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:37:00  来源:igfitidea点击:

How do I perform an IMAP search in Python (using Gmail and imaplib)?

pythongmailimap

提问by Jeremy Dunck

In Gmail, I have a bunch of labeled messages.

在 Gmail 中,我有一堆带标签的邮件。

I'd like to use an IMAP client to get those messages, but I'm not sure what the search incantation is.

我想使用 IMAP 客户端来获取这些消息,但我不确定搜索咒语是什么。

c = imaplib.IMAP4_SSL('imap.gmail.com')
c.list()
('OK', [..., '(\HasNoChildren) "/" "GM"', ...])
c.search(???)

I'm not finding many examples for this sort of thing.

我没有找到很多关于这种事情的例子。

采纳答案by cdleary

imaplibis intentionally a thin wrapper around the IMAP protocol, I assume to allow for a greater degree of user flexibility and a greater ability to adapt to changes in the IMAP specification. As a result, it doesn't really offer any structure for your search queries and requires you to be familiar with the IMAP specification.

imaplib是一个围绕 IMAP 协议的瘦包装器,我假设允许更大程度的用户灵活性和更大的能力来适应 IMAP 规范中的变化。因此,它并没有真正为您的搜索查询提供任何结构,并且要求您熟悉IMAP 规范

As you'll see in section "6.4.4. SEARCH Command", there are many things you can specify for search criterion. Note that you have to SELECTa mailbox (IMAP's name for a folder) before you can search for anything. (Searching multiple folders simultaneously requires multiple IMAP connections, as I understand it.) IMAP4.listwill help you figure out what the mailbox identifiers are.

正如您将在“6.4.4. SEARCH 命令”一节中看到的,您可以为搜索条件指定许多内容。请注意,您必须先找到SELECT邮箱(文件夹的 IMAP 名称),然后才能搜索任何内容。(根据我的理解,同时搜索多个文件夹需要多个 IMAP 连接。)IMAP4.list将帮助您确定邮箱标识符是什么。

Also useful in formulating the strings you pass to imaplibis "9. Formal Syntax" from the RFC linked to above.

在制定传递给的字符串时也很有用的imaplib是来自上面链接的 RFC 的“9. 正式语法”。

The r'(\HasNoChildren) "/"'is a mailbox flag on the root mailbox, /. See "7.2.6. FLAGS Response".

r'(\HasNoChildren) "/"'是在根邮箱的邮箱标志/。参见“7.2.6. 标志响应”。

Good luck!

祝你好运!

回答by Avadhesh

import imaplib 
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login('username', 'password')
obj.select('**label name**') # <-- the label in which u want to search message
obj.search(None, 'FROM', '"LDJ"')

回答by PutzKipa

The easiest way to use imaplib with Gmail is to use the X-GM-RAWattribute as described in the Gmail Imap Extensions page.

将 imaplib 与 Gmail 结合使用的最简单方法是使用 Gmail Imap扩展页面X-GM-RAW中所述的属性。

The process would be like this:

这个过程是这样的:

First connect to the account with the appropriate email and password:

首先使用适当的电子邮件和密码连接到帐户:

c = imaplib.IMAP4_SSL('imap.gmail.com', 993)
email = 'eggs@spam'
password = 'spamspamspam'
c.login(email, password)

Then connect to one of the folders/labels:

然后连接到文件夹/标签之一:

c.select("INBOX")

If necessary, you can list all the available folders/labels with c.list().

如有必要,您可以列出所有可用的文件夹/标签c.list()

Finally, use the search method:

最后,使用搜索方法:

gmail_search = "has:attachment eggs OR spam"
status, data = c.search(None, 'X-GM-RAW', gmail_search)

In the gmail_searchyou can use the same search syntax used in gmail advanced search.

在 中,gmail_search您可以使用与gmail 高级搜索相同的搜索语法 。

The search command will return the status of the command and the ids of all the messages that match your gmail_search.

search 命令将返回命令的状态以及与您的 gmail_search 匹配的所有邮件的 ID。

After this you can fetch each messages by id with:

在此之后,您可以通过 id 获取每条消息:

for id in data[0].split():
    status, data = gmail.fetch(id, '(BODY[TEXT])')

回答by Jeremy Dunck

I've been pretty surprised that imaplib doesn't do a lot of the response parsing. And it seems that responses were crafted to be hard to parse.

我很惊讶 imaplib 没有做很多响应解析。似乎响应被设计得难以解析。

FWIW, to answer my own question: c.search(None, 'GM')

FWIW,回答我自己的问题:c.search(None, 'GM')

(I have no idea what the '(\HasNoChildren) "/"' part is about.)

(我不知道 '(\HasNoChildren) "/"' 部分是关于什么的。)