3 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
5 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
7 # $Id: mailer.py,v 1.5 2007/01/17 16:03:30 faiyaza Exp $
13 FROM="support@planet-lab.org"
15 def email (subject, text, to):
16 """Create a mime-message that will render HTML in popular
17 MUAs, text in better ones"""
22 out = cStringIO.StringIO() # output buffer for our message
23 txtin = cStringIO.StringIO(text)
25 writer = MimeWriter.MimeWriter(out)
27 # set up some basic headers... we put subject here
28 # because smtplib.sendmail expects it to be in the
31 writer.addheader("Subject", subject)
32 if to.__class__ == [].__class__ :
33 writer.addheader("To", to[0])
35 for dest in to[1:len(to)]:
38 writer.addheader("CC", cc)
40 writer.addheader("To", to)
42 writer.addheader("MIME-Version", "1.0")
44 # start the multipart section of the message
45 # multipart/alternative seems to work better
46 # on some MUAs than multipart/mixed
48 writer.startmultipartbody("alternative")
51 # the plain text section
53 subpart = writer.nextpart()
54 subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
55 pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
56 mimetools.encode(txtin, pout, 'quoted-printable')
59 # Now that we're done, close our writer and
60 # return the message body
67 server = smtplib.SMTP(MTA)
68 server.sendmail(FROM, to, msg)
70 except Exception, err:
71 print "Mailer error: %s" % err
73 if __name__=="__main__":
77 id = plc.siteId(["alice.cs.princeton.edu"])
80 #email('TEST', emailTxt.mailtxt.ssh % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
83 #email("TEST109", "THIS IS A TEST", ["faiyaza@cs.princeton.edu", "faiyaz@winlab.rutgers.edu", "faiyaza@gmail.com"])