SEARCH BEFORE/AFTER with Pythons imaplib
Asked Answered
B

2

15

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

Bowker answered 11/4, 2011 at 12:35 Comment(0)
S
22

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")')
Stercoraceous answered 16/2, 2012 at 10:48 Comment(5)
The parens don't hurt, but I did find them unnecessary, at least on gmail.Rivard
The parens are required in some casesIncantatory
The parens are required in some cases, even many cases, but never for since/before, because the date format uses only a-z, 0-9 and -, and none of those need quoting.Sinh
If I could give you 10 votes here, I would. The lack of python docs makes this difficult. You saved the day!Charitycharivari
wow, i'm surprised that switching the quotations from single quotes inside double quotes vs what's here actually mattered; using this as is is important.Ezequiel
C
7

you can try:

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

or if you're using UIDs:

typ, data = M.uid('search', '(SINCE 01-Jan-2010)')
Conch answered 11/4, 2011 at 13:42 Comment(2)
is it possible to also use time with date?Cleaver
@Cleaver no, those commands are done "disregarding time and timezone": tools.ietf.org/html/rfc3501.html#section-6.4.4Wageworker

© 2022 - 2024 — McMap. All rights reserved.