Skip to content

Instantly share code, notes, and snippets.

@chand1012
Created April 3, 2017 19:14
Show Gist options
  • Save chand1012/5c6c0238defe017b1856a81ce6382d0f to your computer and use it in GitHub Desktop.
Save chand1012/5c6c0238defe017b1856a81ce6382d0f to your computer and use it in GitHub Desktop.
import imaplib
import email
from json import loads
def email_login_info(file_name='login.json'):
try:
file_keys = open(file_name)
except:
file_keys = open(file_name, 'w')
raw_json = '''{
"email":"",
"password":""
}
'''
print("Error! JSON file does not exist!")
parsed_json = loads(file_keys.read())
return parsed_json
def get_latest_email():
FROM_EMAIL = email_login_info()['email']
FROM_PWD = email_login_info()['password']
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT = 993
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL,FROM_PWD)
mail.select('inbox')
etype, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
return latest_email_id
starting_email_id = get_latest_email()
print("Starting ID of {}".format(starting_email_id))
while True:
if not starting_email_id == get_latest_email():
print("Email recieved!")
starting_email_id += 1
print("Starting ID is now {}".format(starting_email_id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment