From 78e358e2b89043ebad19955b87d3219092111a88 Mon Sep 17 00:00:00 2001 From: Greg Darke Date: Wed, 13 Feb 2008 23:21:23 +1100 Subject: [PATCH] Initial attempt at adding a fallback server and ssh support --- mail/sendmail.py | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/mail/sendmail.py b/mail/sendmail.py index 67236e2..217ed8d 100755 --- a/mail/sendmail.py +++ b/mail/sendmail.py @@ -1,17 +1,17 @@ #!/usr/bin/env python # Specify SMTP servers here -smtpServerList = [ -(".internode.on.net", "mail.internode.on.net"), -(".usyd.edu.au", "mail.usyd.edu.au"), -(".iinet.net.au", "mail.iinet.net.au"), -(".netspace.net.au", "mail.netspace.net.au"), -(".optusnet.com.au", "mail.optusnet.com.au") -] +smtpServerList = ( + (".internode.on.net", "mail.internode.on.net"), + (".usyd.edu.au", "mail.usyd.edu.au"), + (".iinet.net.au", "mail.iinet.net.au"), + (".netspace.net.au", "mail.netspace.net.au"), + (".optusnet.com.au", "mail.optusnet.com.au"), +) +###DEFAULT_SERVER = None - -import email, email.utils, smtplib, sys, urllib +import email, email.utils, smtplib, sys, urllib, subprocess # Get the to addresses toAddrs = sys.argv[1:] @@ -24,6 +24,7 @@ if len(toAddrs) == 0 or filter(lambda to: to.startswith("-"), toAddrs): print >>sys.stderr, "Usage: %s toAddr ..." % sys.argv[0] sys.exit(1) +print os.environ # Pick a SMTP server try: host = urllib.urlopen("http://suits.ug.it.usyd.edu.au/myip.php").read().strip() @@ -34,17 +35,28 @@ try: else: raise Exception, "No match for hostname: %s" % host except Exception, e: - print >>sys.stderr, "Error! Couldn't pick an SMTP server" - print >>sys.stderr, e - sys.exit(1) + if 'DEFAULT_SERVER' in dir(): + smtpServer = DEFAULT_SERVER + else: + print >>sys.stderr, "Error! Couldn't pick an SMTP server" + print >>sys.stderr, e + sys.exit(1) # Get the from address message = sys.stdin.read() fromAddr = email.message_from_string(message)["from"] fromAddr = email.utils.parseaddr(fromAddr)[1] -# Send the email -smtp = smtplib.SMTP(smtpServer) -smtp.sendmail(fromAddr, toAddrs, message) -smtp.quit() - +if smtpServer is None: + cmdline = ['ssh', 'kagami', '/usr/sbin/sendmail'] + cmdline.extend(toAddrs) + process = subprocess.Popen(cmdline, stdin=subprocess.PIPE) + process.communicate(message) + sys.exit(process.wait()) +else: + # Send the email + smtp = smtplib.SMTP(smtpServer) + smtp.sendmail(fromAddr, toAddrs, message) + smtp.quit() + +sys.exit(1) -- 2.39.2