extract email text body without html content in python poplib

933 Views Asked by At

I need help. This is the email i want to retrieve. Its just a plain text.

enter image description here

But when I retrieve it the html content is being retrieved as well. This is the example:enter image description here

What do i need to do to only retrieve the message inside the yellow box?

My code:

import poplib,os
from email import parser
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('[email protected]')
pop_conn.pass_('mypassword')
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ['\n'.join(map(bytes.decode, mssg[1])) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]

for message in messages:
    sub= message['subject']
    fr=(message['from'])
    for part in message.walk():
        if part.get_content_type():
            body = str(part.get_payload())
            #print(body)
            with open('file_name.txt','r+') as file:
                file.write(sub+'\n')
                file.write(fr+'\n')
                file.write(body+ os.linesep)
                lines=file.readlines()
                lines = [line.rstrip('\n') for line in open('file_name.txt')]
                file.close()
pop_conn.quit()

I tried to use part.get_payload(decode=True)but the html content is still showing also the text message prints as one long straight line.

0

There are 0 best solutions below