import imaplib
import email
import getpass
M = imaplib.IMAP4_SSL("imap.gmail.com")
email = getpass.getpass("Email : ")
password = getpass.getpass("Password : ")
M.login(email, password)
M.select("INBOX")
status, data = M.search(None, "UNSEEN")
email_id = data[0]
result, email_data = M.fetch(email_id, "(RFC822)")
raw_email = email_data[0][1]
raw_email_string = raw_email.decode("utf-8")
email_message = email.message_from_string(raw_email_string)
for i in email_message.walk():
if i.get_content_type() == "text/plain":
body = i.get_payload(decode = True)
print(body)
**Whenever I run this Program I get this error : imaplib.IMAP4.error: FETCH command error: BAD [b’Could not parse command’]. There is no problem with creating a connection with my gmail account. Also the search method works fine, the only problem is the fetch method.Is there any fix for this Problem. I’m new to this module I may have made any silly mistakes **