title: Python: Keep checking new email and alert of further new emails
body: <p>I have this code that checks the latest email and then goes and does something. Is it possible to write something that keeps checking the inbox folder for new mail? Although I want it to keep checking for the latest new email. Is it getting too complicated if I try and store that it has made one pass? So it doesn't alert about the same email twice about the same email.</p>
  
  <p>Code:</p>
  
  <pre><code>import imaplib
  import email
  import Tkinter as tk
  
  word = ["href=", "href", "&lt;a href="] #list of strings to search for in email body
  
  #connection to the email server
  mail = imaplib.IMAP4_SSL('imap.gmail.com')
  mail.login('xxxx', 'xxxx')
  mail.list()
  # Out: list of "folders" aka labels in gmail.
  mail.select("Inbox", readonly=True) # connect to inbox.
  
  result, data = mail.uid('search', None, "ALL") # search and return uids instead
  
  ids = data[0] # data is a list.
  id_list = ids.split() # ids is a space separated string
  latest_email_uid = data[0].split()[-1]
  
  result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') # fetch the email headers and body (RFC822) for the given ID
  
  
  raw_email = data[0][1] # here's the body, which is raw headers and html and body of the whole email
  # including headers and alternate payloads
  
  .....goes and does other code regarding to email html....
  </code></pre>