2

Seems

How to make GMAIL SEARCH with non-ascii strings

Some comment from Google Groups, but can't grasp how to create it with Python and imaplib:

Send non-ASCII data using literals:

. search charset utf-8 x-gm-raw {30}
+ go ahead
"живалов -йцукен"
* SEARCH 586
. OK SEARCH completed (Success)
Pedro Romano
  • 10,973
  • 4
  • 46
  • 50
Vasya
  • 267
  • 1
  • 5
  • 15

2 Answers2

3

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.

Community
  • 1
  • 1
Pedro Romano
  • 10,973
  • 4
  • 46
  • 50
1

My fork of the IMAPClient library adds a gm_search() method which perform's Gmail's X-GM-RAW search query using IMAP literals.

Example:

imap_client.gm_search(u'subject:"ƒoo∫ar" has:attachment', charset='utf8') 
# The method encodes the unicode string internally. I'm debating with myself if that's a good idea or not.

I'm waiting for feedback from the author if it should be included in the main project.

john2x
  • 22,546
  • 16
  • 57
  • 95