3 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
5 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
7 # $Id: mailer.py,v 1.7 2007/04/06 16:16:53 faiyaza Exp $
10 from config import config
14 logger = logging.getLogger("monitor")
17 FROM="monitor@planet-lab.org"
19 def email(subject, text, to):
20 """Create a mime-message that will render HTML in popular
21 MUAs, text in better ones"""
26 if config.mail and config.debug:
29 out = cStringIO.StringIO() # output buffer for our message
30 txtin = cStringIO.StringIO(text)
32 writer = MimeWriter.MimeWriter(out)
34 # set up some basic headers... we put subject here
35 # because smtplib.sendmail expects it to be in the
38 writer.addheader("Subject", subject)
39 if to.__class__ == [].__class__ :
40 writer.addheader("To", to[0])
42 for dest in to[1:len(to)]:
45 writer.addheader("Cc", cc)
47 writer.addheader("To", to)
51 writer.addheader("Bcc", config.email)
53 writer.addheader("Reply-To", 'monitor@planet-lab.org')
55 writer.addheader("MIME-Version", "1.0")
57 # start the multipart section of the message
58 # multipart/alternative seems to work better
59 # on some MUAs than multipart/mixed
61 writer.startmultipartbody("alternative")
64 # the plain text section
66 subpart = writer.nextpart()
67 subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
68 pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
69 mimetools.encode(txtin, pout, 'quoted-printable')
72 # Now that we're done, close our writer and
73 # return the message body
80 # mail and debug, 'to' changed at the beginning'
81 # nomail, but report who I'd send to.
84 # This is normal operation
85 server = smtplib.SMTP(MTA)
86 server.sendmail(FROM, to, msg)
88 except Exception, err:
89 print "Mailer error: %s" % err
91 #print "Would mail %s" %to
92 logger.debug("Would send mail to %s" % to)
94 if __name__=="__main__":
98 email("[spam] This is a mail-test from golf.cs.princeton.edu",
99 "I'm concerned that emails aren't leaving golf. Sorry for the spam",
100 "princetondsl@sites.planet-lab.org")
101 #id = plc.siteId(["alice.cs.princeton.edu"])
104 #email('TEST', emailTxt.mailtxt.ssh % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
107 #email("TEST111", "I'd like to see if this works anywhere", ["soltesz@cs.princeton.edu", "soltesz@cs.utk.edu"])
108 #print "mailer does nothing in main()"