Update to new API.
[monitor.git] / mailer.py
1 #!/usr/bin/python2
2 #
3 # Copyright (c) 2004  The Trustees of Princeton University (Trustees).
4 #
5 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
6 #
7 # $Id: mailer.py,v 1.6 2007/01/24 19:29:44 mef Exp $
8 from emailTxt import *
9 import smtplib
10 import config
11
12 MTA="localhost"
13 FROM="support@planet-lab.org"
14
15 def email (subject, text, to):
16         """Create a mime-message that will render HTML in popular
17         MUAs, text in better ones"""
18         import MimeWriter
19         import mimetools
20         import cStringIO
21
22         out = cStringIO.StringIO() # output buffer for our message 
23         txtin = cStringIO.StringIO(text)
24
25         writer = MimeWriter.MimeWriter(out)
26         #
27         # set up some basic headers... we put subject here
28         # because smtplib.sendmail expects it to be in the
29         # message body
30         #
31         writer.addheader("Subject", subject)
32         if to.__class__ == [].__class__ :       
33                 writer.addheader("To", to[0])
34                 cc = ""
35                 for dest in to[1:len(to)]:
36                         cc +="%s, " % dest
37                 cc = cc.rstrip(", ") 
38                 writer.addheader("CC", cc)
39         else:
40                 writer.addheader("To", to)
41                 
42         writer.addheader("MIME-Version", "1.0")
43         #
44         # start the multipart section of the message
45         # multipart/alternative seems to work better
46         # on some MUAs than multipart/mixed
47         #
48         writer.startmultipartbody("alternative")
49         writer.flushheaders()
50         #
51         # the plain text section
52         #
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')
57         txtin.close()
58         #
59         # Now that we're done, close our writer and
60         # return the message body
61         #
62         writer.lastpart()
63         msg = out.getvalue()
64         out.close()
65         if not config.debug:
66                 try:
67                         print "Mailing %s" %to
68                         server = smtplib.SMTP(MTA)
69                         server.sendmail(FROM, to,  msg)
70                         server.quit()
71                 except Exception, err:
72                         print "Mailer error: %s" % err
73
74 if __name__=="__main__":
75         import smtplib
76         import emailTxt
77         import plc 
78         id = plc.siteId(["alice.cs.princeton.edu"])
79         print id
80         #if id:
81                 #email('TEST', emailTxt.mailtxt.ssh % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
82         #else:
83         #       print "No dice."
84         #email("TEST109", "THIS IS A TEST", ["faiyaza@cs.princeton.edu", "faiyaz@winlab.rutgers.edu", "faiyaza@gmail.com"])