[Tutor] IMAP library

guillermo.fernandez@epfl.ch guillermo.fernandez@epfl.ch
Wed Apr 30 13:39:02 2003


Hi!

I'm trying to understand how the IMAP library works. For that I've copied this program and modified it a little bit:

import getpass, imaplib, string

M = imaplib.IMAP4('mailbox.site.com')
M.login(getpass.getuser(), getpass.getpass())
M.select('INBOX',1)
typ, data = M.search(None, "FROM \"a.friend@site.com\"")
for num in string.split(data[0]):
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
M.logout()

I recieve this error:
$ python test.py
Password:
Traceback (most recent call last):
  File "test.py", line 8, in ?
    typ, data = M.search(None, "FROM \"a.friend@site.com\"")
  File "/usr/lib/python2.1/imaplib.py", line 468, in search
    typ, dat = apply(self._simple_command, (name, charset) + criteria)
  File "/usr/lib/python2.1/imaplib.py", line 844, in _simple_command
    return self._command_complete(name, apply(self._command, (name,) + args))
  File "/usr/lib/python2.1/imaplib.py", line 681, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SEARCH command error: BAD ['Invalid Search criteria']

I've checked the RFC3501 and they say for the search command:
FROM <string>
Messages that contain the specified string in the envelope structure's FROM field.
As I put a string with the e-mail address I'm looking for, I don't get what I've done wrong.

If someone could give me a hand or has any idea of where I could look for an answer... Thanks!

Guille