# mail.py - Logs into your gmail and prints the number of unread emails.
# echo unread mail to unreadMail.txt
# Script can take up to 5 seconds to run
 
import imaplib  # Used to connect to an IMAP4 server.
 
# Default config
imapServer = 'imap.gmail.com'
imapPort = '993'
 
# User config
username = 'YourGmail'
password = 'YourMailPassword'
logfile = '/home/root/Chandelier/unreadMail.txt'
 
# Connect to an IMAP4 server over SSL
obj = imaplib.IMAP4_SSL(imapServer, imapPort)
 
# Login with username and password
obj.login(username,password)	
 
# Select a the 'INBOX' mailbox (default parameter)					
obj.select()					
 
# Search mailbox no (None) charset, criterion:"UnSeen". Will return a tuple, grab the second part, split each string into a list, and return the length of that list:	
unread = len(obj.search(None,'UnSeen')[1][0].split())
 
f = open(logfile, 'w')
f.write(str(unread))
f.close
