I can access my emails stored on GMail via imaplib as follows:
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(username, password)
mail.select(mailbox='INBOX', readonly=True)
_, data = mail.search(None, '(ALL)')
However data only contains the 1790 most recent emails. When I iterate over the mail numbers, data is None for ids above 1790:
for i in itertools.count(start=1):
_, data = mail.fetch(str(i), "RFC822")
How to access all my emails on GMail via Python?
In GMail's webinterface I can search for range queries like this:
"in:anywhere after:2014-01-01 before:2014-01-02"
But Python can not parse such queries:
_, data = mail.search(None, '(ALL "in:anywhere")')
File "/usr/local/lib/python3.4/imaplib.py", line 660, in search
typ, dat = self._simple_command(name, *criteria)
File "/usr/local/lib/python3.4/imaplib.py", line 1134, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/local/lib/python3.4/imaplib.py", line 965, 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']
Range queries should work in theory work somewhat like that according to the docs on Gmail's IMAP extensions. However when I search for a range of dates as in RFC 3501, I get messages that were send within that time frame:
_, data = mail.search(None, '(SINCE "01-Jan-2011" BEFORE "01-Jan-2012")')