#!/usr/pkg/bin/python """send an email message cygwin replacement of standard unix utility assumes localhost is SMTP server """ Copyright = """ mail - send an email message Copyright (C) 2004 John Comeau 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 2 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, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): return False # defined instead by pytest module, use that for debugging # other globals, specific to this program import smtplib, pwd def mail(*args): verbose = False subject = None to_addresses = [] me = pwd.getpwuid(os.geteuid())[0].strip().replace(' ', '_') from_addr = me while type(args[0]) is not types.StringType: args = args[0] state = 'parsing options' for index in range(0, len(args)): if state == 'parsing options': if args[index].startswith('-'): if args[index] == '-v': verbose = True elif args[index] == '-s': state = 'assigning Subject header' elif args[index] == '-F': state = 'assigning From address' # all other options ignored... # note that this has the affect of making CC and BCC addrs "To:" addrs else: for address in args[index].split(','): to_addresses.append(address.strip()) elif state == 'assigning Subject header': subject = args[index] state = 'parsing options' elif state == 'assigning From address': from_addr = args[index] state = 'parsing options' message = "From: %s\r\n" % from_addr for recipient in to_addresses: message += "To: %s\r\n" % recipient.strip() if subject != None: message += "Subject: %s\r\n" % subject message += "\r\n" # end of headers for line in sys.stdin.readlines(): message += line try: server = smtplib.SMTP('localhost') if verbose: server.set_debuglevel(1) server.sendmail(from_addr, to_addresses[0], message) server.quit() except: sys.stderr.write("Failed sending message:\n" + message) raise def main(): """main routine""" mail(sys.argv[1:]) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... main() else: # if you want something to be done on import, do it here; otherwise pass pass