blueFast
blueFast

Reputation: 44501

Python IMAP search using a subject encoded with utf-8

This question is related to question Python IMAP search using a subject encoded with iso-8859-1, but the reply given there is not working for me.

I am doing the following IMAP search in python:

typ, data = self.M.search("utf-8", "(SUBJECT %s)" % u"réception".encode("utf-8"))

And I get the following exception:

...
    typ, data = self.M.search("utf-8", "(SUBJECT %s)" % u"réception".encode("utf-8"))
  File "/usr/local/python/2.7.2/lib/python2.7/imaplib.py", line 625, in search
    typ, dat = self._simple_command(name, 'CHARSET', charset, *criteria)
  File "/usr/local/python/2.7.2/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/local/python/2.7.2/lib/python2.7/imaplib.py", line 905, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
error: SEARCH command error: BAD ['Could not parse command']

Why is that? How can I solve this problem?

Upvotes: 6

Views: 7011

Answers (4)

Vladimir
Vladimir

Reputation: 6756

External lib https://github.com/ikvk/imap_tools supports search by encoded data

from imap_tools import MailBox, A

# get list of emails that subject contains "réception" from INBOX folder
with MailBox('imap.mail.com').login('test@mail.com', 'pwd') as mailbox:
    for msg in mailbox.fetch(A(subject='réception'), charset='utf8'):
        print(msg.subject)

Upvotes: 0

czDalis
czDalis

Reputation: 1

this one works for me

# use the undocumented IMAP4.literal attribute
sock.literal = u"réception".encode('utf-8')
sock.uid('SEARCH', 'CHARSET', 'UTF-8', 'SUBJECT')

Thanks, Lee!

Upvotes: -2

dnozay
dnozay

Reputation: 24344

import imaplib
import getpass
email = "XXXXXXX@gmail.com"

sock = imaplib.IMAP4_SSL("imap.gmail.com", 993)
sock.login(email, getpass.getpass())

# select the correct mailbox...
sock.select()
# turn on debugging if you like
sock.debug = 4

then:

# use the undocumented IMAP4.literal attribute
sock.literal = "réception"
sock.uid('SEARCH', 'CHARSET', 'UTF-8', 'SUBJECT')

Upvotes: 8

Max
Max

Reputation: 11000

u"réception" will need to be wrapped with quotes: u'"réception"', as IMAPLIB will not quote the string for you in the list.

Update: I could not get gmail's IMAP implementation to accept even a quoted string, and had to use IMAP literal syntax. I'm not sure if this is limitation of my encoding using socat, or a limitation with gmail.

a UID SEARCH CHARSET utf-8 SUBJECT "réception"
a BAD Could not parse command

a UID SEARCH CHARSET utf-8 SUBJECT {10}
+ go ahead
réception
* SEARCH
a OK SEARCH completed (Success)

Unfortunately, imaplib does not provide any way to force using of an IMAP literal.

Upvotes: 3

Related Questions