DKM
DKM

Reputation: 1801

Imaplib search for subject line with special characters using python

I'm trying to search special characters subject lines using

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "Report #160496147 : "AU report QTD date wise MediaOps" from Dhruv Sapra"')

but it's raising a bad request command error.

Any idea why its happening and any solution for it.

the below solution is not working.

Python3 IMAP search special characters

Upvotes: 0

Views: 1677

Answers (1)

Max
Max

Reputation: 10985

Your problem is first that you have double quotes within your quoted string, which is a syntax error:

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "Report #160496147 : "AU report QTD date wise MediaOps" from Dhruv Sapra"')

Try escaping your inner quotes:

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "Report #160496147 : \"AU report QTD date wise MediaOps\" from Dhruv Sapra"')

Also note that how SEARCH actually works is implementation specific. Searching for a word or two usually works well, but searching for literal strings often does not, depending on how the server indexes it, if at all. For example, the gmail server indexes words only, and searching for substrings or punctuation usually does not work. If you server is compatible, just searching for the report number with or without the # may or may not give you better results.

typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "#160496147"')
typ, msg_ids = self.imap.uid('search', None, 'SUBJECT "160496147"')

Upvotes: 1

Related Questions