This is a duplicate of Python IMAP search using a subject encoded with utf-8 and after extensive research I have reached the same conclusion there: currently this is impossible to achieve with Python's imaplib
module, because the search
method doesn't support IMAP literals ({<number>}
strings used to denote the literals are quoted by imaplib
). The same aplies to the uid
method, so it can't be used to workaround the search
limitation.
Note that in the past this functionality probably was implemented without using IMAP literals, as can be seen in the question Python IMAP search using a subject encoded with iso-8859-1.
Some hacking of imaplib
is clearly required so search
supports passing the search string as a literal, like in the raw IMAP interaction in your question. A possibility may be to implement it in the context of the imapclient project which may be easier than contributing changes to the Python standard library.