I'm completely new to Python (v2.7) and I need to write a simple program that runs continuously, listens to a gmail account and calls a function when said account receives an email. So far it seems like using either Python's built-in IMAP or SMTP libraries seems like the best option but I'm not very familiar with them, let alone Python itself.
What I've come up with so far is able to connect to the email and find the most recent email via IMAP4 but it's lacking a way to continuously check whether a new email has been received:
import imaplib import email #logs in to the desired account and navigates to the inbox mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('firstname.lastname@example.org','password') mail.list() mail.select('inbox') result, data = mail.search(None,'ALL') #retrieves the latest (newest) email by sequential ID ids = data id_list = ids.split() latest_email_id = id_list[-1]
Any help on how I can adjust this to continuously run and check for new incoming messages would be great. Thanks!