Python IMAP Search with or at a specified email address

I use this with the Gmail SMTP server, and I would like to search through IMAP for emails sent or received from the address.

This is what I have:

mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('user', 'pass') mail.list() mail.select("[Gmail]/All Mail") status, email_ids = mail.search(None, 'TO " tech163@fusionswift.com " OR FROM " tech163@fusionswift.com "') 

Last line of error: imaplib.error: SEARCH command error: BAD ['Could not parse command']

Not sure how I should do this OR statement in python imaplib . If someone can quickly explain what is wrong or point me in the right direction, it would be very helpful.

+6
source share
1 answer

The error you get is generated from the server because it cannot parse the search query correctly. To create the correct query, follow RFC 3501 , on page 49 the structure is explained in detail.

For example, your search string should be correct:

 '(OR (TO " tech163@fusionswift.com ") (FROM " tech163@fusionswift.com "))' 
+14
source

Source: https://habr.com/ru/post/915557/


All Articles