User Tools

Site Tools


python:getemailsgmail

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:getemailsgmail [2013/03/16 17:40] (current)
Line 1: Line 1:
 +==== HOWTO GET EMAILS USING PYTHON AND A GMAIL ACCOUNT ====
 +Version 0.1 29.06.2009
  
 +
 +**Goal**
 +
 +I needed a script who will run into background, and check at some interval my gmail account for new messages. No need to download the messages, just list the statistics about them.
 +
 +
 +**The Script**
 +
 +The script imports the gmail class and grabs (one time only) the user password. This will prevent to store the password somewhere in the file.
 +You can modify the checking interval (default: 60 sec).
 +
 +<code python>
 +#​!/​usr/​bin/​python
 +#
 +
 +import gmail
 +import getpass as gs
 +import time
 +
 +account = '​example_account@gmail.com'​
 +interval = 60 # in seconds
 +
 +pwd = gs.getpass('​Password for %s :> ' % account)
 +
 +while(True):​
 +    a = gmail.Gmail(account,​ pwd)
 +    a.get_messages_list()
 +    time.sleep(interval)
 +</​code>​
 +
 +**Gmail class**
 +
 +Gmail class is using poplib to grab the messages list from any gmail account. \\ 
 +The result looks like this:
 +
 +<code bash>
 +$ python gmail_checker.py
 +Password for example_account@gmail.com :>
 +You have 2 new message(s) with size: 5531
 +
 +
 +Grabbing messages at 19:21:50:
 +
 +
 +Msg#1 From: Cristian NAVALICI <​example_account@gmail.com>​
 + ​Subject:​ [Fwd: OS Reports]
 +Msg#2 From: John ISO <​example_account@gmail.com>​
 + ​Subject:​ Greetings from Romania
 +
 +</​code>​
 +
 +Download the module {{:​python:​gmail.zip| from here}}. Unzip and put it in the same dir as the script above.
python/getemailsgmail.txt ยท Last modified: 2013/03/16 17:40 (external edit)