]> code.delx.au - monosys/commitdiff
Initial attempt at adding a fallback server and ssh support
authorGreg Darke <greg+laptop@tsukasa.net.au>
Wed, 13 Feb 2008 12:21:23 +0000 (23:21 +1100)
committerGreg Darke <greg+laptop@tsukasa.net.au>
Wed, 13 Feb 2008 12:21:23 +0000 (23:21 +1100)
mail/sendmail.py

index 67236e2c335a232812990fa23ef58fd1ee435c09..217ed8d8172f67510ac52069eb38701a182bc802 100755 (executable)
@@ -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)