Last active
April 10, 2017 01:03
-
-
Save bharadwaj-raju/8b135b8ffa28d12e087f to your computer and use it in GitHub Desktop.
Python script/module to check for unread Gmail messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imaplib | |
import re as regex | |
def getGmailUnread(username, password): | |
gmail = imaplib.IMAP4_SSL('imap.gmail.com','993') | |
gmail.login(username,password) | |
gmail.select() | |
unreadCount = regex.search("UNSEEN (\d+)", gmail.status("INBOX", "(UNSEEN)")[1][0]).group(1) | |
unreadCount = int(unreadCount) | |
return unreadCount | |
if __name__ == __main__: | |
unreadCount = getGmailUnread('example_id','passwordexample') | |
if unreadCount == 0: | |
print 'no new mail' | |
elif unreadCount == 1: | |
print '1 new mail' | |
else: | |
print str(unreadCount) + ' new mails' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment