简体   繁体   English

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

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

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. 我没有找到很多这方面的例子。

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. imaplib故意是IMAP协议的一个薄包装器,我假设允许更大程度的用户灵活性和更大的适应IMAP规范变化的能力。 As a result, it doesn't really offer any structure for your search queries and requires you to be familiar with the IMAP specification . 因此,它不会为您的搜索查询提供任何结构,并且需要您熟悉IMAP规范

As you'll see in section "6.4.4. SEARCH Command", there are many things you can specify for search criterion. 正如您将在“6.4.4.SEARCH命令”一节中看到的那样,您可以为搜索条件指定许多内容。 Note that you have to SELECT a mailbox (IMAP's name for a folder) before you can search for anything. 请注意,您必须先SELECT邮箱(文件夹的IMAP名称),然后才能搜索任何内容。 (Searching multiple folders simultaneously requires multiple IMAP connections, as I understand it.) IMAP4.list will help you figure out what the mailbox identifiers are. (根据我的理解,同时搜索多个文件夹需要多个IMAP连接。) IMAP4.list将帮助您确定邮箱标识符的内容。

Also useful in formulating the strings you pass to imaplib is "9. Formal Syntax" from the RFC linked to above. 在制定传递给imaplib的字符串时,也可以imaplib与上面链接的RFC中的“9. Formal Syntax”。

The r'(\\HasNoChildren) "/"' is a mailbox flag on the root mailbox, / . r'(\\HasNoChildren) "/"'是根邮箱上的邮箱标志, / See "7.2.6. FLAGS Response". 参见“7.2.6。标志响应”。

Good luck! 祝好运!

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

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

I've been pretty surprised that imaplib doesn't do a lot of the response parsing. 我很惊讶imaplib没有做很多响应解析。 And it seems that responses were crafted to be hard to parse. 并且似乎反应被精心设计为难以解析。

FWIW, to answer my own question: c.search(None, 'GM') FWIW,回答我自己的问题:c。搜索(无,'GM')

(I have no idea what the '(\\HasNoChildren) "/"' part is about.) (我不知道'(\\ HasNoChildren)'/''部分是什么。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM