import imaplib
import email
import socket
socket.getaddrinfo('127.0.0.1', 8080)
def read_email_from_gmail():
try:
mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)
mail.login('your email', 'your password')
mail.select('inbox')
type, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
typ, data = mail.fetch(bytes(str(id_list), 'utf-8'), '(RFC822)')
for responsepart in data:
if isinstance(responsepart, tuple):
msg = email.message_from_string(responsepart[1].decode('utf-8'))
email_subject = msg['subject']
email_from = msg['from']
print('From : ' + email_from + '\n')
print('Subject : ' + email_subject + '\n')
except Exception as e:
print(str(e))
read_email_from_gmail()
Output: FETCH command error: BAD [b'Could not parse command']
I want to receive a mail from Gmail using Python. Could someone help me where I'm going wrong? and what is this error?
Thank you in advance!