Skip to content

Instantly share code, notes, and snippets.

@bharadwaj-raju
Last active April 10, 2017 01:03
Show Gist options
  • Save bharadwaj-raju/8b135b8ffa28d12e087f to your computer and use it in GitHub Desktop.
Save bharadwaj-raju/8b135b8ffa28d12e087f to your computer and use it in GitHub Desktop.
Python script/module to check for unread Gmail messages
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