Retrieve your unread gmail count

While working on a new device, I was looking at ways to update a display with a bunch of information, including my Unread Email count in my Gmail inbox.

I came across a BASH script which I placed in my mac’s chron file… it retrieves (you select how often) the current quantity of unread emails using Gmail’s Atom functionality. I am using it to curl the information to an Arduino device on my network…

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount="$MAILCOUNTER"&

but you can easily modify it to put data into a variable container or even Text To Speech to your OS or SONOS device. The script is below. It runs on my mac, but I had to alter the original code. I included the original author’s info if your other linux or windows device won’t work with the curl and sed commands as edited.

Have fun with it, I hope some of you find it useful, it is set up for an initialization/error state for me (email count = -1) which you probably won’t need, but you can see where you can edit or add the curl instructions you wish to make…


#!/bin/bash
#SCRIPT_NAME="Arduino Gmail Checker"
#SCRIPT_VERSION="0.1"
#figlet $SCRIPT_NAME $SCRIPT_VERSION
#
#########################################
#                                       #
#    Modified for use with Vera Tool    #
#     BulldogLowell - January 2015      #
# Transmits eMail count to network node #
#                                       #
#########################################################################################
# Description: Check Gmail for unread email and switch RGB LED on Arduino in accordance #
# Dependencies: sed, curl,                                                              #
# Author: Virtualmix                                                                    #
# http://blog.trollmaker.com/article10/arduino-led-notification-for-gmail-on-linux      #
# License: CC BY                                                                        #
#########################################################################################
# Enter Gmail username and password below (Warning: Unsafe storage):
USERID="username"
PASSWORD="password"
# Enter number of seconds between email verification:
WAIT=60
########################################################################################
#set -xv
# Loop to check for new mail every X minutes
while [ "1" -eq "1" ]; do

    # Command line to fetch the number of unread emails:
    MAILCOUNTER=`curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom" \ | xmllint --format - \ | sed -n 's#<fullcount>\(.*\)</fullcount>#\1#p'`
    if [[ "$MAILCOUNTER" = "" ]]; then
        echo "ERROR: The program coulndn't fetch the account for user \"$USERID\"."
        echo "- Are you connected to the Internet?"
        echo -e "- Is the userid and password correct for \"$USERID\"?\n"
        # I'm using this curl....
        curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=-1\&
        # you can do other curl here

    elif [[ "$MAILCOUNTER" -eq "0" ]]; then
        echo "* There is 0 new email for user $USERID."
        # I'm using this curl....
        curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount=0&
        # you can do other curl here....
        
    elif [[ "$MAILCOUNTER" -gt "0" ]]; then
        echo "* There is $MAILCOUNTER new email for user $USERID."
        # I'm using this curl....
        curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET 192.168.1.50/?emailCount="$MAILCOUNTER"&
        # you can do other curl here....
    fi

    echo "* Waiting $WAIT seconds before checking for emails again."
    echo "* (^C to quit the program)"
    sleep $WAIT
#set +xv
done