#!/usr/local/bin/python
# Change the fromaddr and toaddr values below, and change path to python above

import sys,os,re,string,smtplib

#who the response comes from
fromaddr = "secret@localhost"
#who to send the email to
toaddr = "myemail@myhost.com"


input = sys.stdin
mail = input.readlines()

for x in mail:
        #  is string to denote start of command. change it below to suit your needs.
        if re.search("*cmd*",x):
                cmd = string.replace(x,"*cmd*","")
                cmd = string.strip(cmd)
                cmd_in,cmd_out = os.popen4(cmd)
                msg = "Subject: Output from " + cmd + "\r\n"
                msg = msg + cmd_out.read()

#       Optional to send email to whoever sent the original,
#       un-comment out next 7 lines --otherwise, this will
#       only send emails to address configured at above


#       if re.search("From:",x):
#               if re.search("@",x):
#                       who = string.split(x," ")
#                       toaddr = who[2]
#                       toaddr = string.replace(x,"<","")
#                       toaddr = string.replace(x,">","")



#send email
if msg:
        server = smtplib.SMTP('localhost')
        server.sendmail(fromaddr,toaddr,msg)
        server.quit()