3

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")')
4
  • When you give up, try the API Commented Oct 16, 2014 at 3:54
  • As that site states, IMAP should be more "full-fledged". Maybe I should use another imap client than the one from the standard library. Commented Oct 16, 2014 at 4:07
  • Why don't you break it down? Collect 1700, then look at the date and collect from that date to the end of the next 1700? Or just do it one month/year at a time? Commented Oct 16, 2014 at 23:41
  • @JohnMee Thanks, I had that idea already and could make it work, since asking my question. I updated it accordingly. Commented Oct 17, 2014 at 0:04

1 Answer 1

3

As it turns out, there really are no more than about 1700 email in my "INBOX", because I filter most of them away with Gmail's "skip inbox" filter option. Searching only searches in the selected mailbox, so search(None, 'ALL') does not actually get all messages. So I need to iterate over all gmail labels resp. IMAP folders.

There is a list function that gets all IMAP folders:

mail.list()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.