Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Gmail messages using Python IMAP

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 subj: hehe From nobody Tue Dec 25 15:48:52 2018 Content-Type: text/plain; charset="UTF-8"

hello444444444

Bharath Joshi subj: From nobody Tue Dec 25 15:48:52 2018 Content-Type: text/plain; charset="UTF-8"

33333

Bharath Joshi 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?

like image 365
Bharath Joshi Avatar asked Mar 05 '23 20:03

Bharath Joshi


1 Answers

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

like image 50
hansaplast Avatar answered Mar 15 '23 06:03

hansaplast