title: Reading Gmail messages using Python IMAP
body: <p>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.</p>
  
  <p>Code:</p>
  
  <pre><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
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>Bharath Joshi  subj: hehe From nobody Tue
    Dec 25 15:48:52 2018 Content-Type: text/plain; charset="UTF-8"</p>
    
    <p>hello444444444</p>
    
    <p>Bharath Joshi  subj:  From nobody Tue Dec 25
    15:48:52 2018 Content-Type: text/plain; charset="UTF-8"</p>
    
    <p>33333</p>
    
    <p>Bharath Joshi  subj:  From nobody Tue Dec 25
    15:48:53 2018 Content-Type: text/plain; charset="UTF-8"</p>
    
    <p>hello--22</p>
  </blockquote>
  
  <p>The thing which is bothering me is the extra thing I'm getting i.e.</p>
  
  <p>"From nobody ......" and "Content type ...."</p>
  
  <p>How can I get those removed?</p>