0

I'm using imaplib module to parse some of my emails (I'm filtering on FROM field). To do that I'm looping over folders, but seem to miss some emails. When I go to my gmail folder, the emails I miss are exactly those, that don't have "Inbox" label. What the right way to retrieve all of them?

Here's some of my code:

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(*,*)
folders = mail.list()

for f in folders[1]:
    for allfolders in re.findall('"\/"(.*)',f):
        folder = allfolders.replace(" ",'')
        if mail.select(folder)[0] == "OK":
            result, data = mail.search(None, '(FROM "*.*")' )
            do my stuff with data

I've also tried mail.select() but it doesn't help.

UPDATE Problem solved by @Max: need to search in '[Gmail]/All Mail'

LazyCat
  • 496
  • 4
  • 14
  • Are you sure you're not breaking out of the loop prematurely? Are you quoting your folder names? I think `select` doesn't handle folder names with spaces without quoting it yourself. Which I think is a bug in imaplib. – Max Jun 02 '18 at 15:06
  • Thanks, yeah, I'm printing them while debugging. And all the emails are technically in the inbox, though some (exactly those imaplib misses) don't have 'inbox' label. – LazyCat Jun 02 '18 at 23:28
  • 1
    If it doesn't have the inbox label, it's not in the inbox. Maybe you should open the "All Mail" folder instead. – Max Jun 02 '18 at 23:41
  • Thanks a lot! - that's what I needed. Didn't realize I have that folder :) – LazyCat Jun 02 '18 at 23:58
  • Why would they not show up in the loop then? Having a similar problem with a different provider. – Roelant Aug 27 '21 at 10:37

0 Answers0