Search code examples
pythonimapimaplibgmail-imapimap-tools

imaplib.IMAP4.error: SEARCH command error: BAD [b'The specified message set is invalid.'] , not able search with multiple filter's


mail.select("INBOX")
result, messages = mail.search(None, '(AND ("UNSEEN") ("SUBJECT" "important") ("FROM" "[email protected]"))')

When I use OR instead of AND it works fine but when I change it I get this error:

imaplib.IMAP4.error: SEARCH command error: BAD [b'The specified message set is invalid.']

How can I filter muliple fields, like when there are multiple mailIds to filter?

Currntly I am using this:

mail.search(None,'UNSEEN' ,'FROM','"[email protected]"')

with this quary i am not able to filter mulitple From and wants to perform AND OR operations

Versions:

  • python==3.12.0
  • imap-tools==1.7.4

Let me know if you need any extra information.


Solution

  • You're just guessing at the AND syntax, right?

    The defined AND syntax is (strangely) just a parenthesised list of terms. What you want is something like (UNSEEN SUBJECT "important" FROM "[email protected]").