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

时间:2022-04-21 15:03:12

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.

我没有找到很多这方面的例子。

5 个解决方案

#1


8  

imaplib is 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 SELECT a 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.list will help you figure out what the mailbox identifiers are.

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

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

在制定传递给imaplib的字符串时,也可以使用RFC中的“9. Formal Syntax”链接到上面。

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

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

Good luck!

#2


7  

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"')

#3


5  

Labels are accessed exactly like IMAP folders, according to Google.

据Google称,标签的访问方式与IMAP文件夹完全相同。

#4


0  

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。搜索(无,'GM')

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

(我不知道'(\ HasNoChildren)'/“'部分是什么。)

#5


0  

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

将imaplib与Gmail配合使用的最简单方法是使用Gmail Imap Extensions页面中所述的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_search you 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])')

#1


8  

imaplib is 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 SELECT a 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.list will help you figure out what the mailbox identifiers are.

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

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

在制定传递给imaplib的字符串时,也可以使用RFC中的“9. Formal Syntax”链接到上面。

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

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

Good luck!

#2


7  

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"')

#3


5  

Labels are accessed exactly like IMAP folders, according to Google.

据Google称,标签的访问方式与IMAP文件夹完全相同。

#4


0  

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。搜索(无,'GM')

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

(我不知道'(\ HasNoChildren)'/“'部分是什么。)

#5


0  

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

将imaplib与Gmail配合使用的最简单方法是使用Gmail Imap Extensions页面中所述的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_search you 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])')