Once I’ve create a script to monitor application status and send it to my email. And apparently, I need to download all the result to create some graph for analyzing purposes. Luckily, I have python at my disposal, so I can easily fetch email from my Gmail mailbox to some text file and then whipped some awk command to get the stats.

Here’s the simple script to fetch the required emails:

import imaplib
import rfc822
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

imap = imaplib.IMAP4_SSL('imap.gmail.com', 993)
imap.login(USER, PASS)
imap.select()
type,data = imap.search(None, 'SUBJECT', '"Push Stats"')
for num in data[0].split():
    typ, data = imap.fetch(num, '(RFC822)')
    text = data[0][1]
    file = StringIO(text)
    message = rfc822.Message(file)
    print message.fp.read()
imap.close()
imap.logout()

I’m simply run the script and redirect the output to get the text file

python gmail.py > data.txt

And then, just process the data.txt as if its a simple text file data