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

5

12

In Gmail, I have a bunch of labeled messages.

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

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.

Tafoya answered 7/10, 2008 at 15:24 Comment(1)
I am the first comment on the first Python question ever asked!Clift
Z
12

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.

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.

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

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

Good luck!

Zoroastrianism answered 7/1, 2009 at 19:37 Comment(0)
A
9
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"')
Annmaria answered 23/6, 2010 at 11:45 Comment(0)
G
6

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

Gesticulation answered 7/10, 2008 at 16:42 Comment(2)
This is not true. You can't do conn.select("some-label-name").Dissect
@pyrony: Yes, you can. Gmail labels are exposed as top-level IMAP mailboxes. I'm not sure why they decided to do that rather than expose them as IMAP keywords, but that's what Gmail decided to do.Axon
E
3

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

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().

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.

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

After this you can fetch each messages by id with:

for id in data[0].split():
    status, data = gmail.fetch(id, '(BODY[TEXT])')
Eskridge answered 26/4, 2016 at 20:25 Comment(0)
T
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.

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

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

Tafoya answered 9/10, 2008 at 0:38 Comment(4)
This should be incorporated into the original question via an edit. (It is not an answer.)Zoroastrianism
@cdleary: Have you noticed c.search(None, 'GM') part. It might be the wrong answer. But It is an answer.Seta
Oh, missed that. Sorry about that.Zoroastrianism
You mean c.select(None, 'GM').Axon

© 2022 - 2024 — McMap. All rights reserved.