Search code examples
pythonpython-3.ximapimaplib

imaplib.error: SEARCH command error: BAD [b'Could not parse command']


Code: Been getting the error in title but I don't see issue here

def test():
     imap_host = 'imap.gmail.com'
     imap_user = 'email'
     imap_pass = 'pass'

     imap = imaplib.IMAP4_SSL(imap_host)
     imap.login(imap_user, imap_pass)
     imap.select('"[Gmail]/All Mail"')
     message = imap.search(None, 'SUBJECT', "Verify your email")
     print(message)

test()

whats the problem here?


Solution

  • Your subject needs to be in quotes, as passed to the server. You are only quoting it for Python, but not for transmission.

    You are sending SEARCH SUBJECT verify your email (which looks like three broken commands) rather than SEARCH SUBJECT "Verify your email".

    Try imap.search(NONE, 'SUBJECT', '"Verify your email"')

    Note the double quotes inside your string.