"Mail receiver server"

Kartic removethis.kartic.krishnamurthy at gmail.com
Mon Jan 31 20:38:19 EST 2005

manatlan said the following on 1/31/2005 5:46 PM:
> I like the idea, but how to connect the virtual smtp and a python script ?
> but ... There should be a way to be python only ... because i don't want 
> to be very complex, i just want to get the subject of the email, to run 
> some commands (and perhaps get a attachment file) ... but that's all
> i am pretty sure that a recipe exists ... that a man has already done 
> that !
> But i don't know what to query on google ...


What you are trying to do is what is called a mail filter. So google for 
python mail filter and see what you get.

With this setup, lets say Computer B sends an email to user controluser 
on Computer  A , the email is actually passed off to a script (in your 
case python script) to process the incoming email, instead of actually 
delivering the email to the user's inbox on "A".

I don't have the faintest the idea how you would do this on IIS's 
virtual SMTP server; there is probably a setup user panel. In any case, 
on UNIXish systems, there are a few ways of doing it of which one way is 
to setup a user in a file called /etc/aliases and put an entry like this:

controluser: | /usr/local/bin/mailprocessor.py

which tells the SMTP server to pass off the email to script after the | 
(pipe) when controluser gets an email.

So, see if the SMTP control panel provides you with some such feature. 
If IIS does not, my suggestion to you will be to install Cygwin from 
cygwin.com and use the Exim Mail transfer agent available with Cygwin 
(You will have to choose that when installing Cygwin, it is not 
automatically installed) or please google for Windows SMTP Server and 
see if something piques your interest.

Now, coming to the Python related part. Your SMTP server will invoke 
your script by sending the incoming email data on your script's sysin.

So, from python you will do...

import sys, email, os

em = email.message_from_file(sys.stdin) # Read message from Std Input
subject = em.get('Subject')             # Get the subject from em object
if subject.startswith('reboot'):
    os.system('shutdown -r') # reboot the Server

For more information, please read the email module in the Python Module 

Hope that will help you get started!


More information about the Python-list mailing list