Home > python > Check Gmail for new messages

Check Gmail for new messages

Problem
I want to check my new Gmail messages periodically. When I get a message from a specific sender (with a specific Subject), I want to trigger some action. How to do that?

Solution
Fortunately, there is an atom feed of unread Gmail messages at https://mail.google.com/mail/feed/atom. All you have to do it is visit this page, send your login credentials, fetch the feed and process it.

import urllib2

FEED_URL = 'https://mail.google.com/mail/feed/atom'

def get_unread_msgs(user, passwd):
    auth_handler = urllib2.HTTPBasicAuthHandler()
    auth_handler.add_password(
        realm='New mail feed',
        uri='https://mail.google.com',
        user='{user}@gmail.com'.format(user=user),
        passwd=passwd
    )
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)
    feed = urllib2.urlopen(FEED_URL)
    return feed.read()

##########

if __name__ == "__main__":
    import getpass

    user = raw_input('Username: ')
    passwd = getpass.getpass('Password: ')
    print get_unread_msgs(user, passwd)

For reading XML I use the untangle module:

import untangle    # sudo pip install untangle

xml = get_unread_msgs(USER, PASSWORD)
o = untangle.parse(xml)
try:
    for e in o.feed.entry:
        title = e.title.cdata
        print title
except IndexError:
    pass    # no new mail

Links

Advertisements
Categories: python Tags: , , , ,
  1. February 10, 2018 at 15:32

    Hello Jabba,
    I was looking into python untangled and your examples in this blog helped me understand the untangled module a bit. Here is the scenario I am facing:
    This is my test.xml

    import untangle
    
    obj = untangle.parse('test.xml')
    
    def asset_name():
        """
        Function to return the asset name
        """
        for assetname in obj.some_service.service_info:
            print ("Asset name is:", str(assetname['name']))
    
    def db_details():
        """
        Function to return the db details for all items under the nonxa
        """
        for dbname in obj.some_service.object[1].item[0]:
            print ("DB name is:", dbname['db_user'])
    

    asset_name() –> output is test (This works correctly)

    db_details() –> (I am able to get only db_user because I specified object[1].item[0] in the db_details() function but I want all the details like db_name, db_user, etc for both db_name- SID1 and SID2)

    I tried (for dbname in obj.some_service.object:) but I am not able to loop through properly.
    Any suggestions on how to loop through the objects and print all db details out?

    Thanks & Regards,
    Prasad

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: