Though I did most of it after searching a lot from lots of sites I am still not able to get the correct output which I wanted.
Code:
import imaplib
import smtplib
import email
mail=imaplib.IMAP4_SSL("imap.gmail.com")
mail.login("**************@gmail.com","********")
mail.select('inbox')
type,data=mail.search(None,'ALL')
mail_ids=data[0]
id_list=mail_ids.split()
for i in range(int(id_list[-1]),int(id_list[0])-1,-1):
typ,data=mail.fetch(i,'(RFC822)')
for response_part in data :
if isinstance(response_part,tuple):
msg=email.message_from_string(response_part[1])
email_from=msg['from']
email_subj=msg['subject']
c=msg.get_payload(0)
print email_from
print "subj:",email_subj
print c
Output:
Bharath Joshi <bharathjoshi99@gmail.com> subj: hehe From nobody Tue Dec 25 15:48:52 2018 Content-Type: text/plain; charset="UTF-8" hello444444444 Bharath Joshi <bharathjoshi99@gmail.com> subj: From nobody Tue Dec 25 15:48:52 2018 Content-Type: text/plain; charset="UTF-8" 33333 Bharath Joshi <bharathjoshi00@gmail.com> subj: From nobody Tue Dec 25 15:48:53 2018 Content-Type: text/plain; charset="UTF-8" hello--22
The thing which is bothering me is the extra thing I'm getting i.e.
"From nobody ......" and "Content type ...."
How can I get those removed?