#!/usr/bin/python """ mailer This program reads in a list of file names and creates a multi-part MIME mail message, then mails it to a specified recipient, via the specified mail host. If no recipient is specified, mail is sent to yourself. If no mailhost is specified, use localhost. Usage: mailer [] ... [ -t | --to ] Send the message to this recipient [yourself]. This value may be a comma separated list for multiple recipients. This is most efficient method for multiple receivers. [ -l | --list ] read in the named file as a list of recipients, one per line. The file may contain comments with "#". [ -f | --from ] Mail marked as being FROM this address [user@hostname]. [ -m | --mailhost ] Send SMTP to this mailhost [localhost]. [ -s | --subject ] Set the Subject line. [No Subject] [ -d | --debug ] print some verbose debug messages. [ -V | --version ] Display program version. [ -h | -? | --help ] Display this usage message. The MIME type is taken from the file's extension. Make sure that the files you send have a valid extension, and that your mime.types file is valid. Plain text files (.txt) will be sent un-encoded, all other files will be base64 encoded. """ import sys, os, pwd import getopt, string from cStringIO import StringIO from smtplib import SMTP from mailerlib import * revision = "$Id: $" shortArgs = "f:t:m:l:s:Vd?h" longArgs = [ "help", "version", "mailhost=", "from=", "to=", "list=", "subject=", "debug" ] # defaults debug = 0 smtphost = "localhost" subject = "No subject" fromaddr = pwd.getpwuid(os.getuid())[0] + "@" + os.uname()[1] toaddrs = [] # process options optlist, sys.argv[1:] = getopt.getopt(sys.argv[1:], shortArgs, longArgs) for arg in optlist: if arg[0] == "-h" or arg[0] == "-?" or arg[0] == "--help": print __doc__ sys.exit(0) if arg[0] == "-V" or arg[0] == "--version": print "Version: ", revision sys.exit(0) if arg[0] == "-d" or arg[0] == "--debug": debug = 1 if arg[0] == "-m" or arg[0] == "--mailhost": smtphost = arg[1] if arg[0] == "-f" or arg[0] == "--from": fromaddr = arg[1] if arg[0] == "-t" or arg[0] == "--to": for clist in string.split(arg[1], ","): toaddrs.append(clist) if arg[0] == "-l" or arg[0] == "--list": for clist in read_to_list(arg[1]): toaddrs.append(clist) if arg[0] == "-s" or arg[0] == "--subject": subject = arg[1] # default recipient is yourself (gotta send it somewhere...) if toaddrs == []: toaddrs = [pwd.getpwuid(os.getuid())[0]] # *** Start of main program if debug: print "Would send message from ", fromaddr print "with these files attached: " for file in sys.argv[1:]: print " ", file print "\nTo these recipients:" for to in toaddrs: print " ", to print "\nvia this host: ", smtphost else: if len(sys.argv[1:]) == 0: print "Error: no files to send. Use -h for help." sys.exit(1) # buld message in memory, then mail it via SMTP outputfp = StringIO() build_message(outputfp, sys.argv[1:], subject) # now send the email message using built-in SMTP maildest = SMTP(smtphost) maildest.sendmail(fromaddr, toaddrs, outputfp.getvalue()) maildest.quit() outputfp.close()