Here's a little python script that I wrote to monitor my IMAP mail
inbox.  It works by checking periodically to see if any new unread
messages have appeared.  I use upstart on an Ubuntu 8.04 server to keep
it running all the time.

In my case, I'm using the script to pass the new e-mail on to myself as
a cell phone text message.

The script is pasted in below and also in github:
git://github.com/dc25/imaptomsg.git

- Dave


#! /usr/bin/python
import imaplib, time
import os
from tempfile import NamedTemporaryFile

msgEmail = "" # text message email address here.
loginName = "" # imap login name ; gmail uses [EMAIL PROTECTED]
loginPassword = "" # imap password

def textMessage(msg):
    filehandle = NamedTemporaryFile()
    filehandle.write(msg)
    filehandle.flush()
    systemCmd = "mutt " + msgEmail + " > /dev/null 2>&1 < " + filehandle.name
    os.system(systemCmd)
    filehandle.close()


imap = imaplib.IMAP4_SSL('imap.gmail.com', 993)
imap.login(loginName, loginPassword)
imap.select()
typ, msgnums = imap.search(None, 'UNSEEN')
unseen0 = set(msgnums[0].split())
while 1:
    time.sleep(20)
    imap.select()
    typ, msgnums = imap.search(None, 'UNSEEN')
    unseen1 = set(msgnums[0].split())
    for n in unseen1 - unseen0:
        t, hdr_from    = imap.fetch(n, '(BODY.PEEK[HEADER.FIELDS (FROM)])')
        t, hdr_subject = imap.fetch(n, '(BODY.PEEK[HEADER.FIELDS (SUBJECT)])')
        t, text        = imap.fetch(n, '(BODY.PEEK[TEXT])')
        textMessage( hdr_from[0][1] + hdr_subject[0][1] + text[0][1])
    unseen0 = unseen1

imap.close()
imap.logout()

~
_______________________________________________
EUGLUG mailing list
[email protected]
http://www.euglug.org/mailman/listinfo/euglug

Reply via email to