Search code examples
pythonemailimapimaplib

Find messages by different criteria IMAP.search()


I use the imaplib library to work with emails. How can I search for messages with several parameters using IMAP4.search()?
For example, find all unread messages that came after a specific date, with the subject "Subject", from the sender "test@testmail.com".
I had an idea to make an 'one by one' search, I mean to search with next parameter in the result of the previous one, but:

    typ, data = con.search(None, '(UNSEEN)')

Result stored in list and I can't use search() function to that list. Any ideas?


Solution

  • 'UNSEEN FROM "test@testmail.com" SUBJECT "Subject" SINCE 17-may-1814' should be what you want. All that is actually case insensitive, upper case is often used for keywords but it doesn't matter.

    You'll find many, many examples in older SO questions about this.