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

How to fetch Text message of email in python using imaplib [closed]

$
0
0

I am new in python and currently i am working on a project to read emails using imap in python. Right now i am able to read email header but the body is not readable ,It is not in the text format. I want to read the messages in clean text format. Here is my code below .Thanks in advance.


import imaplib
import base64
import email
import sys


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

M = imaplib.IMAP4_SSL('imap.gmail.com',993)
M.login(email_user, email_pass)
M.select("Inbox")

typ, message_numbers = M.uid('search',None, 'ALL')  # change variable name, and use new name in for loop

for num in message_numbers[0].split():
    typ, data = M.uid('fetch' , num , 'RFC822')
    if typ != 'OK':
        print("ERROR getting message", num)


    msg = email.message_from_bytes(data[0][1])
    hdr = email.header.make_header(email.header.decode_header(msg['Subject']))
    sub = str(hdr)
    content = str(msg.get_payload()[0])
    print('Message %s: %s' % (num, sub))
    print('Comming from', msg['From'])
    #print('Content:' + str(msg.get_payload()[0]))
    print('Message %s: %s' %(num, content))




M.close()
M.logout()

Viewing all articles
Browse latest Browse all 29919

Trending Articles



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