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

后端 未结 1 439
栀梦
栀梦 2021-01-06 19:02

From a different account, I sent myself an email with the subject Test de réception en local. Now using IMAP, I want to find that email searching by subject.

相关标签:
1条回答
  • 2021-01-06 19:57

    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")
    
    0 讨论(0)
提交回复
热议问题