Python3 IMAP search special characters

2019-09-03 10:28发布

问题:

I would like to find a note over IMAP using Python3 and it's IMAP extension. I try to search for a subject which contains special chars as in the example below. Is there a way I can encode the search string to be IMAP compatible?

self.connection.search(None, 'ALL', 'SUBJECT "büro"')

returns

UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 10: ordinal not in range(128)

回答1:

You should pass UTF-8 encoded string as the search argument and also need to mention "CHARSET UTF-8" in the search query.

some thing like this UID SEARCH CHARSET utf-8 "search string". Here "search string" should be utf-8 encoded.