Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29762

How do I grab the email 'body" in gmail using imaplib and email module?

$
0
0

How do I grab the email 'body" in gmail using imaplib and email module? As you can see, I am able to grab the "From" and the "Subject". When I try and print the message, it prints the raw message. I want it without the html and everything else.

See Code Below

import imaplibimport email# Gmail Credentialsusername = 'yourGmail'password = 'yourGmailPassword'def read_email_from_gmail():        mail = imaplib.IMAP4_SSL('imap.gmail.com')        mail.login(username,password)        mail.select('inbox')        result, data = mail.search(None, 'ALL')        mail_ids = data[0]        id_list = mail_ids.split()           first_email_id = int(id_list[0])        latest_email_id = int(id_list[-1])        for i in range(latest_email_id,first_email_id, -1):            # need str(i)            result, data = mail.fetch(str(i), '(RFC822)' )            for response_part in data:                if isinstance(response_part, tuple):                    # from_bytes, not from_string                    msg = email.message_from_bytes(response_part[1])                    email_from = msg['from']                    email_subject = msg['subject']                    print ('From : '+ email_from +'\n')                    print ('Subject : '+ email_subject +'\n')                    print ('Message : '+ str(msg) +'\n')                    print ('-----------------------------------------------------')# nothing to print hereread_email_from_gmail()

Viewing all articles
Browse latest Browse all 29762

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>