title: How to get unread messages and set message read flags over IMAP using Python?
body: <pre><code>import imaplib
  def read():
  
      userName = "xxx@gmail.com"
      password = "xxxx" 
      name = 'xxx@gmail.com'
      email_ids = [userName]
      data = []
      imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
      imap_server.login(userName, password)
      imap_server.select('INBOX')
      da = []
      status, response = imap_server.status('INBOX', "(UNSEEN)")
      unreadcount = int(response[0].split()[2].strip(').,]'))
      print unreadcount
  
      status, response = imap_server.search(None, '(FROM "xxx@gmail.com")')
      email_ids = [e_id for e_id in response[0].split()]
      for e_id in email_ids:
          _, response = imap_server.fetch(e_id, '(UID BODY[TEXT])')
          da.append(response[0][1])
      print da
  
  
  read()
  </code></pre>
  
  <p>How to organise the code above, to read only unread mails?
  Also, once we read them, how to mark the messages as read mail using Python?</p>