Search code examples
pythongmailgmail-imap

getting emails from gmail via python


I'm using the following code to download all my emails from gmail, but unfortunately, the total number of emails returned does not match the total number of emails in the account. In particular, I'm able to get the first 43 messages, but I count 20+ more in the inbox that are missed. Perhaps this is some sort of limit on the number that can be pulled back(?). Thanks in advance for any assistance provided!

import imaplib, email, base64

def fetch_messages(username, password):
    messages = []
    conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    conn.login(username, password)
    conn.select()
    typ, data = conn.uid('search', None, 'ALL')

    for num in data[0].split():
        typ, msg_data = conn.uid('fetch', num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                messages.append(email.message_from_string(response_part[1]))
        typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
    return messages

Solution

  • I use the following to get all email messages.

    resp,data = mail.uid('FETCH', '1:*' , '(RFC822)')
    

    and to get all the ids I use:

    result, data = mail.uid('search', None, "ALL")
    print data[0].split()
    

    gives:

    ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ... etc ]
    

    EDIT

    In my case the follow returns 202 dates which is in excess of what the OP is looking for and is the correct number.

    resp,data = mail.uid('FETCH', '1:*' , '(RFC822)')
    messages = [data[i][1].strip() for i in xrange(0, len(data), 2)] 
    for msg in messages:
        msg_str = email.message_from_string(msg)
        print msg_str.get('Date')