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