Gmail notification

In my last blog I wrote how to use notify-send to make a balloon notification from the command line. The reason I researched that subject, was that I wanted to make a Bash-script that runs on cron to check if there are unread emails on my Gmail and Google Apps accounts, and if there are pop-up a bubble to notify me. There are of course several such applications already. But it wanted to do it myself as an excuse to play with Google's mail feed, which I recently became aware of. In this blog post, I will explain how you can make such script yourself.
Read the feed
Gmail or Google Apps accounts has a feed of new and unread mail associated with them. The feed has following URL:
https://mail.google.com/mail/feed/atom
You also need to supply the email address and password that you use to login at your Gmail or Google Apps account.
We will use curl to read the feed within the script. Assuming your email address and password are stored in the variables $user and $password, for instance bob@gmail.com and secret, respectively, it looks as follow.
user=bob@gmail.com pass=secret curl -s -u $user:$pass https://mail.google.com/mail/feed/atom
You could of course use wget as well.
user=bob@gmail.com pass=secret wget -q -O - --user=$user --password=$pass https://mail.google.com/mail/feed/atom
Parse the feed
Next, we need to parse the feed to find out if there is any unread email and how many they are. A look at the feed shows that the element <fullcount>...</fullcount> has all information we need. Let's employ Perl to extract the number of unread mail, if any, into the variable $m. For test purpose, we also print a message if there is any mail.
user=bob@gmail.com pass=secret m=$(curl -s -u $user:$pass https://mail.google.com/mail/feed/atom | \ perl -ne 'print $1 if /<fullcount>(.*)<\/fullcount>/ && $1 > 0') if [ -n "$m" ]; then echo "$user has $m mail"; fi
Notify the user
Now, replace echo above with notify-send, which I has described in an earlier blog, to pop-up a balloon (or bubble) with the mail notification.
user=bob@gmail.com pass=secret m=$(curl -s -u $user:$pass https://mail.google.com/mail/feed/atom | \ perl -ne 'print $1 if /<fullcount>(.*)<\/fullcount>/ && $1 > 0') if [ -n "$m" ]; then notify-send -i mail-unread "$user has $m mail"; fi
Put everything together
Let's put everything together, clean it up and save it as the script ~/bin/gmail-notify:
#!/usr/bin/env bash ### EDIT FOLLOWING TWO VARIABLES ### # The email address you use to login to Gmail or Google Apps. user=bob@gmail.com # The password you use to login to Gmail or Google Apps. pass=secret ### DON'T EDIT BELOW THIS LINE ### m=$(curl -s -u $user:$pass https://mail.google.com/mail/feed/atom | \ perl -ne 'print $1 if /<fullcount>(.*)<\/fullcount>/ && $1 > 0') if [ -n "$m" ] then notify-send -i mail-unread "You have $m mail" fi
Since it contains sensitive information, make it readable only by you, and make it executable at the same time.
chmod a=,u=rwx ~/bin/gmail-notify
You can now test it.
~/bin/gmail-notify
Cron
Finally, enter crontab -e on the command line, to add following line to your personal crontab:
*/12 * * * * env DISPLAY=:0 ~/bin/gmail-notify > /dev/null 2>&1
This makes the script run every twelfth minute. The redirection of stderr to stdout (2>&1) and stdout to /dev/null (> /dev/null) is to prevent cron to spam you with email containing stderr and stdout. The env DISPLAY=:0 portion is necessary for cron to know that is should use current display. Make sure that the crontab ends with a blank line, or otherwise the last entry will never run. For more information on how to setup cron, see CronHowto.
Improved script that checks several accounts
Since I have Google Apps accounts both for my NodeOne email address and my private email address, I'm running following generalized script myself. I have added some more options to notify-send, although they currently are not honored. The screenshot at the beginning of this blog shows this script in action.
#!/usr/bin/env bash
#
# Gmail notify.
#
# By Thomas Barregren <thomas@barregren.se>.
### EDIT THE FOLLOWING TWO LISTS ###
# List of email addresses used to login to Gmail or Google Apps.
user=(bob@gmail.com bob@example.com)
# List of passwords used to login to Gmail or Google Apps.
pass=(secret private)
### DON'T EDIT BELOW THIS LINE ###
for ((i=0; i<${#user[*]}; ++i))
do
m=$(curl -s -u ${user[$i]}:${pass[$i]} https://mail.google.com/mail/feed/atom | \
perl -ne 'print $1 if /<fullcount>(.*)<\/fullcount>/ && $1 > 0')
if [ -n "$m" ]
then
msg="${user[$i]} has $m mail\n$msg"
fi
done
if [ -n "$msg" ]
then
notify-send -u low -c "email.arrived" -i mail-unread "You have mail" "$(echo -e $msg)"
fi
Post new comment