Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

1. Only show a received email once until a new one is received
2. Only show the new one once
3. Repeat forever

What I have tried:

I have tried using a variable called raw_email_c. It worked like this:
raw_email = data[0][1]
if raw_email_c == raw_email:
continue
raw_email_c = raw_email
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900