Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python imaplib search with multiple criteria

I'm trying to use the search function and am running into an issue. I can download all attachments from a gmail account and sort them according to the file extension. I have all of that code working right except when I add extra criteria to the search. Originally the search criteria was only for UNSEEN emails, which works and then flags the email as seen and moves it to the trash. I then decided to add to it. Here is the example: original:

resp, items = m.search (None, 'UNSEEN') 

new:

resp, items = m.search (None, '(FROM "email" SUBJECT "some text")', 'UNSEEN')

It results with the emails moved to the trash, still unread and none of the attachments downloaded. Anyone have any idea what I may be doing wrong here? Thanks.

like image 806
Jeff Avatar asked Apr 21 '16 21:04

Jeff


1 Answers

To build on JithPS comment the right syntax is like this:

result, data = mail.search(None,'(FROM "email" SUBJECT "the subject" UNSEEN)')

The clauses are passed with capital letters and the criteria within quotes.

like image 81
Claudio Mazzoni Avatar answered Oct 23 '22 04:10

Claudio Mazzoni