Jeff
Jeff

Reputation: 103

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.

Upvotes: 7

Views: 14170

Answers (1)

Claudio Mazzoni
Claudio Mazzoni

Reputation: 167

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.

Upvotes: 8

Related Questions