How to Make a Gmail Notifier in Python

Jan 19, 2012 08:04 AM

In this article, I'll show you how to make a simple Gmail notifier. Python can do various things in terms of notifications; sending commands to an Arduino unit, playing sounds, opening windows, etc. The code below simply plays some music, but the possibilities of notification methods are endless. If you aren't familiar with python, there are many sites with tutorials like this one, or this one

What You Need

  • A Gmail account
  • Python 2.7 or later, with feedparser installed (get it here)
  • A little python experience

The Code

For this program, you need two pieces of code. The first one parses your account data and sends it to Gmail, then checks for mail. The second portion runs this program every second, and notifies you when a new email is present. The code I wrote simply plays three notes using winsound (it will only work on Windows computers).

First portion- Parser.py:

import sys, feedparser, winsound

#Here, we import modules needed for this program

newEmail=""

USERNAME="YOUR.GMAIL.ADDRESS@gmail.com"

PASSWORD="YOUR PASSWORD"

PROTO="https://"

SERVER="mail.google.com"

PATH="/gmail/feed/atom"

#We assign variables with values. Fill in your username and password

def mail(checker):

    email = int(feedparser.parse(

        PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH

    )["feed"]["fullcount"])

#parses your account data and sends it to gmail

    if email > 0:

        newEmail = 1

    else:

        newEmail = 0

    #checks for mail

    if newEmail==1:

         winsound.Beep(440, 500)

         winsound.Beep(370, 500)

         winsound.Beep(392, 500)

#plays sound if email present

Second portion- Checker.py:

import parser2, time

#import modules

x=1

#set variable

while True:

    parser2.mail(0)

    time.sleep(10)

#re-run program to infinity, every ten seconds. Change the "time.sleep(seconds to wait) variable if you want a longer/shorter check interval.

Other Possibilities

I simply have it playing sound, but it would take all of three seconds to set up an Arduino interface, and in turn, have the Arduino light up a bulb, ring a physical bell, or any number of things. You could even have python speak the email text, or reply to the sender. Use your imagination! :)



Comments

No Comments Exist

Be the first, drop a comment!