SEARCH BEFORE/AFTER with Pythons imaplib

Martin Hansen picture Martin Hansen · Apr 11, 2011 · Viewed 20.4k times · Source

I have a smaller IMAP-script written i Python(3.2).

I my search-line looks like this:

typ, data = M.search(None, 'FROM', '"MyName"')

I get the expected results. However, if I change it to something like:

typ, data = M.search(None, 'AFTER', '"01-Jan-2010"')

(with or without quoted date, I get this error

Traceback (most recent call last):
  File "./priv/imap.py", line 100, in <module>
    main()
  File "./priv/imap.py", line 93, in main
    print(to_json(fetch_result(M, args), args))
  File "./priv/imap.py", line 51, in fetch_result
    typ, data = M.search(None, 'AFTER', '"01-Jan-2010"')
  File "/usr/lib/python3.2/imaplib.py", line 652, in search
    typ, dat = self._simple_command(name, *criteria)
  File "/usr/lib/python3.2/imaplib.py", line 1121, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python3.2/imaplib.py", line 957, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SEARCH command error: BAD [b'Could not parse command']

I have no idea why that would be illegal, but all help will be appreciated! Also, what I ultimatly want to do is use "YOUNGER 1234567" in order to do some finer filtering, but I'm not sure if gmail/python supports this yet.

thanks in advance

Answer

Green Su picture Green Su · Feb 16, 2012

You can use search like these:

But seems it does not support detailed time, but only date.

and the date is the internal date (disregarding time and timezone) of the email

M.search(None, '(SINCE "01-Jan-2012")')
M.search(None, '(BEFORE "01-Jan-2012")')
M.search(None, '(SINCE "01-Jan-2012" BEFORE "02-Jan-2012")')