IMAP4 search with special characters

Luis Corrales l.c at visotech.at
Fri Jul 21 03:51:21 EDT 2006


Hi all,

I have a problem when searching for text with special characters in
e-mails in an IMAP server. I'm using imaplib in python 2.4.3 and I can't
get this code working:

# first connect and login
conn = IMAP4(my_server)
conn.login(my_user, my_pass)

# now select INBOX
conn.select('INBOX')

# and search for messages (the first parameter of "search" is the
encoding, which defaults to None)
conn.search(None, '(ALL)')          # output: ('OK', ['1 2 3'])

# That works perfectly and I get the 3 messages that I have in my Inbox:
# Now I want to search for messages with special characters. This works
(still no special characters in my search):

conn.search(None, '(BODY gasse)')

# and even this:

conn.search('ISO-8859-1', '(BODY gasse)')

# but not this (there comes the 'ö'):

conn.search('ISO-8859-1', '(BODY Lemböckgasse)')    # error: SEARCH
command error: BAD ['Bogus criteria list in SEARCH']

# After having read the IMAP4 documentation I thought this could work:

conn.search('ISO-8859-1', '(CHARSET ISO-8859-1 BODY Lemböckgasse)')

# I tried everything that came up to my mind, getting always the same
error. For instance:

conn.search(None, '(CHARSET ISO-8859-1 BODY Lemböckgasse)')
conn.search(None, '(BODY Lemb\xf6ckgasse)')
conn.search(None, '(BODY Lemböckgasse)'.encode('ISO-8859-1'))   # here I
get the error "can't decode byte 0xf6 in position 10"
conn.search('ISO-8859-1', '(BODY Lemböckgasse)'.encode('ISO-8859-1'))

And so on. Does anybody have the remotest idea what I'm doing wrong and
how I could find my e-mail?

Thanks in advance,

Luis Corrales



More information about the Python-list mailing list