How to read email content in Python 3

Viewed 33903

I've tried many code to access and read email content, for example, about Gmail i only can do authentication and with Outlook i have code that can read email but it's encrypted...but now it only access email and doesn't output with encrypted information. So i need help to solve this. Thanks

Here's the code:

import imaplib
import base64 

email_user = input('Email: ')
email_pass = input('Password: ')

M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
M.login(email_user, email_pass)
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    num1 = base64.b64decode(num1)
    data1 = base64.b64decode(data)
    print('Message %s\n%s\n' % (num, data[0][1]))
M.close()
M.logout()
3 Answers

use data[0][1].decode('utf-8')

It will work. I was also facing the same problem. It worked for me.

You just need to enable the less secure apps in your Gmail account if it is not enabled you will get an authentication error even if your code is all right. I was also getting so I researched and got this error

Related