5

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?

Flimm
  • 136,138
  • 45
  • 251
  • 267
Bharath Joshi
  • 53
  • 1
  • 1
  • 4

1 Answers1

7

Ah, the "beauty" of emails… Apparently you're facing multipart email messages and for these, the get_payload() method is also outputting the headers. You'd need to use msg.walk() like so:

for response_part in data :
    if isinstance(response_part,tuple):
        msg=email.message_from_string(response_part[1])
        print "subj:", msg['subject']
        print "from:", msg['from']
        print "body:"
        for part in msg.walk():
            if part.get_content_type() == 'text/plain':
                print part.get_payload()

For a more complete answer have a look at this stackoverflow answer

hansaplast
  • 11,007
  • 2
  • 61
  • 75
  • @BharathJoshi please accept the answer if it solves your use case, otherwise explain what is still unanswered – hansaplast Dec 25 '18 at 21:51
  • the `fetch()` method seems to return both tuples (what you actually are looking for) and strings, e.g. `)` (which I don't fully understand why, [here](https://stackoverflow.com/questions/2230037/how-to-fetch-an-email-body-using-imaplib-in-python) is some discussion about this). If you don't check that response_part is a tuple, you'll run into issues when doing response_part[1] because when the string has just size 1 you'll run into string index out of range issue which you posted – hansaplast Dec 28 '18 at 13:16