pythonemailimapclient

Imapclient module not reading emails PROPERLY


I am using python3 and, using my email credentials, this function prints a block of text that appears to be divided into formal sections. Here is my code for reading emails:

import pprint
import imapclient

# Email = Your email
# Password = Your password
# folder = just type '[Gmail]/All Mail'.
# readonly = Tell wether emails are marked as read or not when you read them here.
# search = Search term for gmail. Can be empty by typing ''.
def ReadEmail(Email, Password, folder, readonly, search):
    imapObj = imapclient.IMAPClient('imap.gmail.com', ssl=True)
    imapObj.login(' ' + Email + ' ', ' ' + Password + ' ')
    pprint.pprint(imapObj.list_folders())
    imapObj.select_folder(str(folder), readonly=readonly)
    UIDs = imapObj.gmail_search(str(search))
    rawMessages = imapObj.fetch(UIDs, ['BODY[]'])
    pprint.pprint(rawMessages)

This email can be accessed through less secure apps by the way, by google's settings. Anyway, when you run this with your credentials, you get this big block of text. The side notes tell you what is what, and the quotes were there in my script to remind me where things are:

                #    {1: {b'BODY[]': b'Bcc: thedestoryer11@gmail.com\r\nReturn-Path: '      <<< Receiver's email
                #               b'<noreply.smtp.supermechm500@gmail.com>\r\nRece'           <<< Sender of an email
                #               b'ived: from [127.0.0.1] (ec2-23-21-200-247.comput'
                #               b'e-1.amazonaws.com. [23.21.200.247])\r\n       '
                #               b' by smtp.gmail.com with ESMTPSA id 24-v6sm169089'         <<< Sent using...
                #               b'38qts.19.2018.11.14.16.31.38\r\n        for <t'
                #               b'hedestoryer11@gmail.com>\r\n        (version=T'           <<< Reciever of the email
                #               b'LS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=12'
                #               b'8/128);\r\n        Wed, 14 Nov 2018 16:31:38 -'
                #               b'0800 (PST)\r\nMessage-ID: <5becbe6a.1c69fb81.4'
                #               b'af4f.c041@mx.google.com>\r\nDate: Wed, 14 Nov '           <<< Date Received
                #               b'2018 16:31:38 -0800 (PST)\r\nFrom: noreply.smt'
                #               b'p.supermechm500@gmail.com\r\n\r\nTesting from SM'         <<< Message (Will not contain buttons or images. Check your email application for these media
                #               b'TP +-+\r\n',

I can see the \n, \r and b'' in there but it doesn't function. It's just printed out completely. How do I print this formally?


Solution

  • The imapclient docs demonstrate how to create email message objects using Python's email package:

    import email
    
    for uid, message_data in server.fetch(messages, 'RFC822').items():
        email_message = email.message_from_bytes(message_data[b'RFC822'])
        print(uid, email_message.get('From'), email_message.get('Subject'))
    

    If you want to print a complete message you could do

    print(email_message)
    

    The email.message.EmailMessage class provides various methods for accessing different parts of a message such as headers, body, mime parts.