#! /usr/bin/env python '''Emulate the mail-sending portion of the unix Mail program.''' # # Do imports with care in case we are loaded as a module. # import getopt _getopt = getopt del getopt import smtplib _smtplib = smtplib del smtplib import socket _socket = socket del socket import sys import time _time = time del time def send_message (subject, recipients, msgbody): '''Send e-mail message.''' envelope_from = "d0-release-mgr@fnal.gov" envelope_to = recipients.split (",") # Construct the message. msg = _format_msg (subject, recipients, msgbody) # Send the message. _send_message_via_smtp (envelope_from, envelope_to, msg) def _usage (msg): '''Command line help.''' sys.exit (sys.argv[0] + ": usage: " + msg) def _main (): '''Command line executive.''' envelope_from = "d0-release-mgr@fnal.gov" # Get the command line options and arguments. try: (opts, args) = _getopt.getopt (sys.argv[1:], "s:") except (_getopt.error), msg: _usage (msg) # The -s option specifies the optional subject. subject = "" for (opt, arg) in opts: if opt == "-s": subject = arg # There must be exactly one argument which is the recipient list. if not args: _usage ("you must specify at least one recipient!") if len (args) > 1: _usage ("only one argument expected!") # Process the recipient list. recipients = args[0] envelope_to = recipients.split (",") # Get the message body from stdin. msgbody = "" for line in sys.stdin.xreadlines (): msgbody = msgbody + line # Construct the message. msg = _format_msg (subject, recipients, msgbody) # Send the message. _send_message_via_smtp (envelope_from, envelope_to, msg) def _format_msg (subject, recipients, msgbody): '''Add a RFC822 header to the message.''' # Get the current date in the RFC822 format. curtime = _time.time () localtime = _time.localtime (curtime) daylight = localtime[8] gmtime = _time.gmtime (curtime) if daylight: tz = -_time.altzone tzname = _time.tzname[1] else: tz = -_time.timezone tzname = _time.tzname[0] tz_hr = tz / 3600 tz_mn = tz % 3600 tz_mn = tz_mn / 60 curdate = _time.strftime ("%a, %d %b %Y %H:%M:%S ", localtime) curdate = curdate + "%+03d%02d (%s)" % (tz_hr, tz_mn, tzname) # Makeup a message id using the current time and the hostname. hostname = _socket.gethostname () if hostname.find (".") == -1: (prim_name, alias_names, ipaddrs) = _socket.gethostbyaddr (hostname) if not hostname: hostname = prim_name if prim_name.find (".") != -1: hostname = prim_name else: for alias in alias_names: if alias.find (".") != -1: hostname = alias break msgid = "<%08x@%s>" % (curtime, hostname) # Ok, we can make the message header now. msg = "" msg = msg + "Date: %s\n" % (curdate,) msg = msg + "From: D0 Release Manager \n" msg = msg + "Subject: %s\n" % (subject,) msg = msg + "To: %s\n" % (recipients,) msg = msg + "Errors-to: d0-release-mgr-owner@fnal.gov\n" msg = msg + "Message-id: %s\n" % (msgid,) msg = msg + "Organization: Fermi National Accelerator Laboratory\n" msg = msg + "X-Mailer: PyMail v1.0\n" msg = msg + "MIME-version: 1.0\n" msg = msg + "Content-type: text/plain; charset=us-ascii\n" msg = msg + "Content-transfer-encoding: 7bit\n" msg = msg + "X-Accept-Language: en\n" msg = msg + "\n" msg = msg + msgbody return msg def _send_message_via_smtp (envelope_from, envelope_to, msg): '''Send and e-mail message via SMTP.''' server = _smtplib.SMTP () # server.set_debuglevel (1) server.connect ("smtp.fnal.gov") server.sendmail (envelope_from, envelope_to, msg) server.quit () return 0 if __name__ == "__main__": # We have been called as a script. _main () sys.exit (0) else: # We are loading as a module. pass