IMAP4 search with special characters

Discussion in 'Python' started by Luis Corrales, Jul 21, 2006.

  1. 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
    Luis Corrales, Jul 21, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    32,985
    Stefan Mueller
    Jul 23, 2006
  2. Max M

    IMAP4.search by message-id ?

    Max M, Mar 30, 2005, in forum: Python
    Replies:
    2
    Views:
    383
    Sean Dodsworth
    Mar 30, 2005
  3. Tony Meyer

    RE: IMAP4.search by message-id ?

    Tony Meyer, Mar 31, 2005, in forum: Python
    Replies:
    1
    Views:
    853
    Sean Dodsworth
    Mar 31, 2005
  4. Laszlo Nagy

    IMAP4 SEARCH question

    Laszlo Nagy, Dec 8, 2006, in forum: Python
    Replies:
    0
    Views:
    494
    Laszlo Nagy
    Dec 8, 2006
  5. Laszlo Nagy

    Re: IMAP4 SEARCH question

    Laszlo Nagy, Dec 8, 2006, in forum: Python
    Replies:
    0
    Views:
    391
    Laszlo Nagy
    Dec 8, 2006
Loading...

Share This Page