How to read email content in Python 3

30,445

Solution 1

I've checked through your code anyway, and there are a couple of comments. The first one is that commas seem to have been stripped out when you pasted the code in here. I've tried to put them back, so check that my code does match yours. Remember that without access to Outlook, I can't test my suggestions.

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)')   # data is being redefined here, that's probably not right
    num1 = base64.b64decode(num1)          # should this be (num) rather than (num1) ?
    data1 = base64.b64decode(data)
    print('Message %s\n%s\n' % (num, data[0][1]))  # don't you want to print num1 and data1 here?

M.close()
M.logout()

Assuming that's correctly reconstituted, you call your message list data on line 10, but then re-assign the results of calling fetch() to data on line 13.

On line 14, you decode num1, which hasn't been defined yet. I'm not sure that the numbers need decoding though, that seems a bit strange.

On line 16, you're printing the encoded values, not the ones you've decoded. I think you may want something like

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, message_numbers = M.search(None, 'ALL')  # change variable name, and use new name in for loop

for num in message_numbers[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    # num1 = base64.b64decode(num)          # unnecessary, I think
    print(data)   # check what you've actually got. That will help with the next line
    data1 = base64.b64decode(data[0][1])
    print('Message %s\n%s\n' % (num, data1))

M.close()
M.logout()

Hope that helps.

Solution 2

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

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

Share:
30,445
Ricardo91
Author by

Ricardo91

Updated on July 09, 2022

Comments

  • Ricardo91
    Ricardo91 almost 2 years

    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()
    
  • Mostafa Ayaz
    Mostafa Ayaz over 5 years
    Nice answer. But when retrieving the messages, the result is messy. For example----Message b"\r\xe9b\xbd\xea\xdeu:\x1a\xc9\xac\xe6\xa2\xcbZ}\xa8&j)\\\x‌​a2d^q\xe8\xafy\xd6\x‌​f2\xdbM6k\xd7v\xdb~\‌​xf4\xd3M4\xc2+aH\xc4‌​\xcf\x89\xda9\xda\xf‌​e\x9c\xb2\x9d\xb5\xd‌​3^=\xda\x8bZJ\xe9\xf‌​6\xdd'\xa9\xdbM|\xdb‌​nz\xe3\xad;\xd3C\xc3‌​Mq\xa8\xa2\t^JkiJ\x8‌​b\xabq\xe0\x02pez\xd‌​9sE\xe8\xb2\xee\x1b\‌​xcd\xf2\x85$\x11J\xd‌​d$\x9a'Z\x83\xb1\x0e‌​7\xf2}+\xd5\xe7_a\xc‌​7g\xd4\" ------this was supposed to be some retrieved email while I can't read anything