0

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

DKM
  • 1,761
  • 2
  • 19
  • 34
  • I don't see any special characters in your example, nor what you tried when you used "the below solution." – Max Dec 14 '18 at 18:43
  • @Max Report #160496147 the # is a special character. I've changed the None to CHARSET UTF-8 while trying the solution given but its not working too – DKM Dec 15 '18 at 15:18
  • In the sense of special characters for encoding, usually that means anything not in ASCII. However, you have nested quotes, which will be a problem. Try using \" for your inner quotes. – Max Dec 16 '18 at 15:24

1 Answers1

1

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"')
Max
  • 10,701
  • 2
  • 24
  • 48