imaplib.IMAP4.error: SEARCH command error: BAD [b'The specified message set is invalid.'] code example

Example: imaplib.error: FETCH command error: BAD [b'Could not parse command'

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("[email protected]","mypassword")
m.select('"[Gmail]/All Mail"')

result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
    for num in data[0].split():
    result, data = m.uid('fetch', num, '(RFC822)')
    if result == 'OK':
        email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
        print('From:' + email_message['From'])

m.close()
m.logout()

Tags:

Misc Example