Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot search sent emails in Gmail with Python

I am trying to search for messages in the Sent (actually i care for both) but I only get incoming messages. For the time being i have

imap_conn.select()
str_after = after.strftime('%d-%b-%Y')
typ, msg_ids = imap_conn.search('UTF-8','SINCE',str_after)

Which gives equivalent results with this

imap_conn.select('INBOX')

When I replace INBOX with ALL or SENT I get: command SEARCH illegal in state AUTH, only allowed in states SELECTED

like image 425
PanosJee Avatar asked Jul 16 '10 17:07

PanosJee


3 Answers

Make sure you use the extra quotes in your string:

imap_conn.select('"[Gmail]/Sent Mail"')  

That worked for me.

like image 54
Max Quant Avatar answered Sep 26 '22 02:09

Max Quant


Need to use print imap_conn.list(). Tags are language based. for example in spanish is [Gmail]/Todos

like image 41
Felix Martinez Avatar answered Sep 26 '22 02:09

Felix Martinez


Man, the error message is so misleading. What it's really saying is that you have tried to select an invalid folder name hence the search operation fails.

To verify/check the current valid folders/labels do something like:

Using ImapClient

from imapclient import IMAPClient
## Connect, login and select the INBOX
imap_conn = IMAPClient('imap.gmail.com', use_uid=True, ssl=ssl)
imap_conn.login(USERNAME, PASSWORD)

print(imap_conn.list_folders())

Using imaplib

import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]', 'mypassword')
print(mail.list())

After I could see what folder names it was expecting, all was well.

like image 31
w-- Avatar answered Sep 22 '22 02:09

w--