3 from types import StringTypes
4 from subprocess import Popen, PIPE
6 from PLC.Debug import log
7 from PLC.Faults import *
9 def sendmail(api, To, Subject, Body, From = None, Cc = "", Bcc = "", DSN = "never"):
11 Uses sendmail (must be installed and running locally) to send a
12 message to the specified recipients. If the API is running under
13 mod_python, the apache user must be listed in e.g.,
14 /etc/mail/trusted-users.
16 If dsn is not 'never' (e.g., 'failure', 'delay', or 'success'),
17 then the current support address (PLC_MAIL_SUPPORT_ADDRESS) will
18 receive any delivery status notification messages.
23 From = "%s Support <%s>" % \
24 (api.config.PLC_NAME, api.config.PLC_MAIL_SUPPORT_ADDRESS)
26 header = {'From': From,
27 'version': sys.version.split(" ")[0],
30 # Accept either a string or a list of strings for each of To, Cc, and Bcc
31 for line in 'To', 'Cc', 'Bcc':
32 addresses = locals()[line]
33 if isinstance(addresses, StringTypes):
34 header[line] = addresses
36 header[line] = ", ".join(addresses)
38 if not api.config.PLC_MAIL_ENABLED:
39 print >> log, "From: %(From)s, To: %(To)s, Subject: %(Subject)s" % header
42 p = Popen(["sendmail", "-N", DSN, "-t", "-f" + api.config.PLC_MAIL_SUPPORT_ADDRESS],
43 stdin = PIPE, stdout = PIPE, stderr = PIPE)
47 Content-type: text/plain
53 X-Mailer: Python/%(version)s
56 """.lstrip() % header)
67 raise PLCAPIError, err