I am trying to login to gmail and read the messages from python package. But getting an Error : [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
import imaplib
import email
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("[email protected]","password")
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()
Just turn this ON for your Gmail account - https://myaccount.google.com/lesssecureapps