I've been making a Python script which checks for emails since logging in. Here is my code so far:
#!/usr/bin/python
import imaplib, getpass
mail = imaplib.IMAP4_SSL('imap.gmail.com')
u = raw_input('Your Gmail Address: ')
p = getpass.getpass()
mail.login(u, p)
mail.select("inbox")
while 1:
r, data = mail.search(None, "ALL")
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
r, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
print raw_email
The problem is that it keeps showing the same email over and over again (until a new one is received) because of the while loop.
How can I make it:
- Only show a received email once until a new one is received
- Only show the new one once
- Repeat forever