Python IMAP search using a subject encoded with iso-8859-1

北战南征 提交于 2019-11-30 23:44:55

When talking to IMAP servers, check with IMAP RFC.

You must remove extra quotes, and you must not encode the strings. Also, charset specifies the charset of the search query, not the charset of the message header. This should work (works for me):

M.search("utf-8", "(SUBJECT %s)" % u"réception".encode("utf-8"))
# this also works:
M.search("iso8859-1", "(SUBJECT %s)" % u"réception".encode("iso8859-1"))

Edit:

Apparently some servers (at least gmail as of August 2013) support utf-8 strings only when sent as literals. Python imaplib has a very limited literal arguments support, the best one can do is something like:

term = u"réception".encode("utf-8")
M.literal = term
M.search("utf-8", "SUBJECT")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!