+ monitor.py -- modified the following three to use a record-based events,
[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.7 2007/04/06 16:16:53 faiyaza Exp $
8 from emailTxt import *
9 import smtplib
10 from config import config
11 import logging
12
13 config = config()
14 logger = logging.getLogger("monitor")
15
16 MTA="localhost"
17 FROM="monitor@planet-lab.org"
18
19 def email(subject, text, to):
20         """Create a mime-message that will render HTML in popular
21         MUAs, text in better ones"""
22         import MimeWriter
23         import mimetools
24         import cStringIO
25
26         if config.mail and config.debug:
27                 to = [config.email]
28
29         out = cStringIO.StringIO() # output buffer for our message 
30         txtin = cStringIO.StringIO(text)
31
32         writer = MimeWriter.MimeWriter(out)
33         #
34         # set up some basic headers... we put subject here
35         # because smtplib.sendmail expects it to be in the
36         # message body
37         #
38         writer.addheader("Subject", subject)
39         if to.__class__ == [].__class__ :       
40                 writer.addheader("To", to[0])
41                 cc = ""
42                 for dest in to[1:len(to)]:
43                         cc +="%s, " % dest
44                 cc = cc.rstrip(", ") 
45                 writer.addheader("Cc", cc)
46         else:
47                 writer.addheader("To", to)
48
49         if config.bcc:
50                 print "Adding bcc"
51                 writer.addheader("Bcc", config.email)
52
53         writer.addheader("Reply-To", 'monitor@planet-lab.org')
54                 
55         writer.addheader("MIME-Version", "1.0")
56         #
57         # start the multipart section of the message
58         # multipart/alternative seems to work better
59         # on some MUAs than multipart/mixed
60         #
61         writer.startmultipartbody("alternative")
62         writer.flushheaders()
63         #
64         # the plain text section
65         #
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')
70         txtin.close()
71         #
72         # Now that we're done, close our writer and
73         # return the message body
74         #
75         writer.lastpart()
76         msg = out.getvalue()
77         out.close()
78         # three cases:
79         #       mail but no-debug
80         #       mail and debug, 'to' changed at the beginning'
81         #   nomail, but report who I'd send to.
82         if config.mail:
83                 try:
84                         # This is normal operation
85                         server = smtplib.SMTP(MTA)
86                         server.sendmail(FROM, to,  msg)
87                         server.quit()
88                 except Exception, err:
89                         print "Mailer error: %s" % err
90         else:
91                 #print "Would mail %s" %to
92                 logger.debug("Would send mail to %s" % to)
93
94 if __name__=="__main__":
95         import smtplib
96         import emailTxt
97         import plc 
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"])
102         #print id
103         #if id:
104                 #email('TEST', emailTxt.mailtxt.ssh % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
105         #else:
106         #       print "No dice."
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()"