3 # Copyright (c) 2004 The Trustees of Princeton University (Trustees).
5 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
7 # $Id: mailer.py,v 1.8 2007/06/29 12:42:22 soltesz Exp $
10 from config import config
16 logger = logging.getLogger("monitor")
19 FROM="monitor@planet-lab.org"
21 def reformat_for_rt(text):
22 lines = text.split("\n")
25 spaced_text += " %s\n" %line
29 def emailViaRT(subject, text, to):
30 """Use RT command line tools to send email.
31 return the generated RT ticket ID number.
35 if config.mail and config.debug:
38 os.environ['PATH'] = os.environ['PATH'] + ":/home/soltesz/rpm/opt/rt3/bin/"
39 os.environ['RTSERVER'] = "https://rt.planet-lab.org/"
40 os.environ['RTUSER'] = "monitor"
41 os.environ['RTPASSWD'] = "ssorcmor"
42 os.environ['RTDEBUG'] = "0"
43 # NOTE: AdminCc: (in PLC's RT configuration) gets an email sent.
44 # This is not the case (surprisingly) for Cc:
45 input_text = "Subject: %s\n"
46 input_text += "Requestor: monitor@planet-lab.org\n"
47 input_text += "id: ticket/new\n"
48 input_text += "Queue: Monitor\n"
50 input_text += "AdminCc: %s\n" % recipient
51 input_text += "Text: %s"
53 spaced_text = reformat_for_rt(text)
55 # print input_text % (subject, "")
57 if config.mail and not config.debug:
58 cmd = "rt create -i -t ticket"
59 (f_in, f_out, f_err) = os.popen3(cmd)
60 f_in.write(input_text % (subject, spaced_text))
65 # TODO: rt doesn't write to stderr on error!!!
67 raise Exception, f_err.read()
69 i_ticket = int(value.split()[2])
70 # clean up the child process.
71 f_in.close(); del f_in
72 f_out.close(); del f_out
73 f_err.close(); del f_err
75 elif config.mail and config.debug:
76 email(subject, spaced_text, to)
83 def email(subject, text, to):
84 """Create a mime-message that will render HTML in popular
85 MUAs, text in better ones"""
90 if config.mail and config.debug:
93 out = cStringIO.StringIO() # output buffer for our message
94 txtin = cStringIO.StringIO(text)
96 writer = MimeWriter.MimeWriter(out)
98 # set up some basic headers... we put subject here
99 # because smtplib.sendmail expects it to be in the
102 writer.addheader("Subject", subject)
103 if to.__class__ == [].__class__ :
104 writer.addheader("To", to[0])
106 for dest in to[1:len(to)]:
109 writer.addheader("Cc", cc)
111 writer.addheader("To", to)
113 if config.bcc and not config.debug:
114 writer.addheader("Bcc", config.email)
116 writer.addheader("Reply-To", 'monitor@planet-lab.org')
118 writer.addheader("MIME-Version", "1.0")
120 # start the multipart section of the message
121 # multipart/alternative seems to work better
122 # on some MUAs than multipart/mixed
124 writer.startmultipartbody("alternative")
125 writer.flushheaders()
127 # the plain text section
129 subpart = writer.nextpart()
130 subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
131 pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
132 mimetools.encode(txtin, pout, 'quoted-printable')
135 # Now that we're done, close our writer and
136 # return the message body
144 # mail and debug, 'to' changed at the beginning'
145 # nomail, but report who I'd send to.
148 # This is normal operation
149 server = smtplib.SMTP(MTA)
150 server.sendmail(FROM, to, msg)
151 if config.bcc and not config.debug:
152 server.sendmail(FROM, config.email, msg)
154 except Exception, err:
155 print "Mailer error: %s" % err
157 #print "Would mail %s" %to
158 logger.debug("Would send mail to %s" % to)
160 if __name__=="__main__":
164 #email("[spam] bcc test from golf.cs.princeton.edu",
165 # "It gets to both recipients",
166 # "soltesz@cs.utk.edu")
167 #emailViaRT("rt via golf",
168 # "It gets to both recipients",
169 # "soltesz@cs.utk.edu")
170 id = emailViaRT("TEST 7",
171 mailtxt.newbootcd_one[1] % {'hostname_list':"hostname list..."},
172 ["soltesz@cs.utk.edu", "soltesz@romcross.org", "soltesz@cs.princeton.edu"])
173 print "ticketid: %d" % id
174 #id = plc.siteId(["alice.cs.princeton.edu"])
177 #email('TEST', emailTxt.mailtxt.ssh % {'hostname': "ALICE.cs.princeton.edu"}, "tech-" + id + "@sites.planet-lab.org")
180 #email("TEST111", "I'd like to see if this works anywhere", ["soltesz@cs.princeton.edu", "soltesz@cs.utk.edu"])
181 #print "mailer does nothing in main()"