I want to fetch from,subject and body of an email using message_numbers of an email. I don't want any attachments or images. Just plain text of body. Can someone please help me with some code snippet? I am stuck at this point.
This is the current code snippet I am using to get the data:
import imaplib
import config
import bs4
import email
imap = imaplib.IMAP4_SSL(config.imap_server,config.imap_port)
r, d = imap.login(config.username, config.password)
imap.select("Inbox")
r, d = imap.uid('search', None, "ALL")
message_numbers = d[0].decode('utf8').split(' ')
for msg_uid in message_numbers:
r, d = imap.uid('FETCH', msg_uid, '(RFC822)')
try:
raw_email = d[0][1].decode('utf8')
except:
raw_email = str(bs4.BeautifulSoup(d[0][1],'lxml'))
email_message = email.message_from_string(raw_email)
print(email_message) # here i need only subject,from and body in string format and i dont want attachments