Check if there’s unread email in a gmail account

I needed an easy way to check if there’s unread email in a gmail account.

import lxml.etree
import requests

def check_for_new_mail(user, password):
    """Check a gmail atom feed for new email"""
    response = requests.get('https://mail.google.com/mail/feed/atom',
                         auth=(user, password))
    ns = {"feed": "http://purl.org/atom/ns#"}
    xml = lxml.etree.fromstring(str(response.text))
    return len(xml.xpath('//feed:entry', namespaces=ns)) > 0
Advertisement
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s