''' Copyright (C) 2008 Mats Taraldsvik This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see ''' #!/usr/bin/python import sys, re import smtplib from email.MIMEText import MIMEText ################## VARIABLES ####### SMTPSERVER = 'smtp.server.com' SENDERADR = "Name Surname " TOADR = 'example2 mail2 com' ################################### def send_mail (senderadr , destinationadr , subject , contents, server='localhost') : mail = MIMEText(contents, _charset="utf-8") # Set right encoding here. mail['From'] = senderadr mail['Subject'] = subject mail['To'] = destinationadr smtp = smtplib.SMTP(server) smtp.sendmail(senderadr , [ destinationadr ], mail.as_string()) smtp.close() message = sys.stdin.readlines() #read the mail that is parsed from stdin headerflag = False contentstring = '' for line in message : if headerflag : contentstring += line.strip('\n') # Collects content in a string if not headerflag : if line == '\n' : # If the last item in header list is a newline character, this works headerflag = True #regexp = re.compile(r'From:(.*)[\s\S]Title:(.*)[\s\S]Text:') #Regex to grab text #if regexp.search(contentstring).groups()[0] and regexp.search(contentstring).groups()[1] : # regex_subject = regexp.search(contentstring).groups()[1] + ' [ ' + regexp.search(contentstring).groups()[0] + ' ] ' #else : regex_subject = "It's Learning ( Message )" send_mail(SENDERADR, TOADR, regex_subject, contentstring , SMTPSERVER)