tags:

views:

1428

answers:

6

Hi all,

I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?

Thank you, Sasha

+1  A: 

I found a pretty good snippet when I wanted to do this same thing (and the example uses gmail). Also check out the google search results on this.

Lucas McCoy
+4  A: 

Gmail provides an atom feed for new email messages. You should be able to monitor this by authenticating with py cURL (or some other net library) and pulling down the feed. Making a GET request for each new message should mark it as read, so you won't have to keep track of which emails you've read.

Dana the Sane
+3  A: 

While not Python-specific, I've always loved procmail wherever I could install it...!

Just use as some of your action lines for conditions of your choice | pathtoyourscript (vertical bar AKA pipe followed by the script you want to execute in those cases) and your mail gets piped, under the conditions of your choice, to the script of your choice, for it to do whatever it wants -- hard to think of a more general approach to "trigger actions of your choice upon receipt of mails that meet your specific conditions!! Of course there are no limits to how many conditions you can check, how many action lines a single condition can trigger (just enclose all the action lines you want in { } braces), etc, etc.

Alex Martelli
Procmail really won't solve the problem of triggering an action (unless the action in question is sending a message).
J. Peterson
@J. Peterson, you ARE kidding, right?! `| pathtoyourscript` and you can start your script of choice and pipe your mail to it when your favorite conditions are satisfied, and the script can do ANYTHING it wants -- how is that NOT "triggering an action"?!
Alex Martelli
+1  A: 

I recently solved this problem by using procmail and python

Read the documentation for procmail. You can tell it to send all incoming email to a python script like this in a special procmail config file

:0:
| ./scripts/ppm_processor.py

Python has an "email" package available that can do anything you could possibly want to do with email. Read up on the following ones....

from email.generator import Generator
from email import Message
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.mime.multipart import MIMEMultipart
ראובן
+1  A: 

People seem to be pumped up about Lamson:

http://lamsonproject.org/

It's an SMTP server written entirely in Python. I'm sure you could leverage that to do everything you need - just forward the gmail messages to that SMTP server and then do what you will.

However, I think it's probably easiest to do the ATOM feed recommendation above.

Adam Nelson
+2  A: 

Gmail provides the ability to connect over POP, which you can turn on in the gmail settings panel. Python can make connections over POP pretty easily:

import poplib
from email import parser

pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('username')
pop_conn.pass_('password')
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print message['subject']
pop_conn.quit()

You would just need to run this script as a cron job. Not sure what platform you're on so YMMV as to how that's done.

mazelife
Thank you very much!!!It works great. The only problem I have now is that I need to retrieve only new messages. Of course it is possible to retrieve all messages and then compare the old list with the new one. However I am wondering if there is a better way to do that, if there were a built in method for that.
Sasha
In POP mode, gmail will only let you retrieve a message once. Subsequent connections will not show you the message. Unless one of 2 things is happening: do you have "recent mode" turned on? See here: http://mail.google.com/support/bin/answer.py?answer=47948 on how to disable that.Alternatively, if gmail is actually giving you the same email each time, it may be because the quit() method is not getting called on your pop connection. Is there an exception perhaps that's being raised but not caught before calling quit? Then gmail will give you the same mail.
mazelife