Created
May 27, 2015 07:47
-
-
Save alessandrocucci/d665f28cbc084786d87e to your computer and use it in GitHub Desktop.
Mail script to check unseen messages in every user's mailbox
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
__author__ = 'Alessandro Cucci' | |
__email__ = '[email protected]' | |
__date__ = '26/05/15' | |
import urllib2 | |
import base64 | |
from xml.dom.minidom import parse | |
# Valid commands to display author and subject for incoming mails | |
INPUT = ['yes', 'YES', 'yep', 'YEP', 'y', 'Y'] | |
# Config dict. Edit this to check every GMail mailbox you want. | |
CONFIG = { | |
'mail': { | |
'Gmail': { | |
'username': '', | |
'password': '' | |
}, | |
'Eneergee3': { | |
'username': '', | |
'password': '' | |
}, | |
'SomeOtherMailbox': { | |
'username': '', | |
'password': '' | |
}, | |
}, | |
} | |
class GMail(): | |
''' | |
GMAIL CLASS | |
Try to connect to every mailbox in CONFIG['mail'] dict and display | |
informations about number of unseen mails, author and subject. | |
Should work with every mailbox with IMAP enabled, but I've not test that yet. | |
''' | |
def __init__(self): | |
self.get_email() | |
def get_text(self, nodelist): | |
rc = [] | |
for node in nodelist: | |
if node.nodeType == node.TEXT_NODE: | |
rc.append(node.data) | |
return ''.join(rc) | |
def get_dom(self, username, password): | |
b64auth = base64.encodestring("%s:%s" % (username, password)) | |
auth = "Basic " + b64auth | |
req = urllib2.Request("https://mail.google.com/mail/feed/atom/") | |
req.add_header("Authorization", auth) | |
handle = urllib2.urlopen(req) | |
dom = parse(handle) | |
handle.close() | |
return dom | |
def count_new_mail(self, dom): | |
count_obj = dom.getElementsByTagName("fullcount")[0] | |
return int(count_obj.firstChild.wholeText) | |
def read_mail(self, dom): | |
for entry in dom.getElementsByTagName("entry"): | |
author = entry.getElementsByTagName("author")[0] | |
name = author.getElementsByTagName("name")[0] | |
print "Mail from:", self.get_text(name.childNodes) | |
subject = entry.getElementsByTagName("title")[0] | |
print "Subject:", self.get_text(subject.childNodes) | |
def get_email(self): | |
AUTHENTICATION = CONFIG['mail'] | |
for item in AUTHENTICATION: | |
try: | |
dom = self.get_dom(AUTHENTICATION[item]['username'], AUTHENTICATION[item]['password']) | |
print item | |
count = self.count_new_mail(dom) | |
if count == 0: | |
print "Nothing to read, sir" | |
elif count == 1: | |
print "You have a new mail, sir." | |
input = raw_input("Should I print some details? ") | |
if input in INPUT: | |
self.read_mail(dom) | |
else: | |
print "You have:", count, "new mails, sir." | |
input = raw_input("Should I print some details? ") | |
if input in INPUT: | |
self.read_mail(dom) | |
except: | |
print "I cannot connect to", item, "mailbox." | |
if __name__ == '__main__': | |
gmail = GMail() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment