4 from email.mime.text import MIMEText
5 from email.header import Header
6 from smtplib import SMTP
8 from PLC.Logger import logger
9 from PLC.Faults import *
11 def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None):
13 Uses sendmail (must be installed and running locally) to send a
14 message to the specified recipients. If the API is running under
15 mod_python, the apache user must be listed in e.g.,
16 /etc/mail/trusted-users.
18 To, Cc, and Bcc may be addresses or lists of addresses. Each
19 address may be either a plain text address or a tuple of (name,
24 if not isinstance(To, list):
26 if Cc is not None and not isinstance(Cc, list):
28 if Bcc is not None and not isinstance(Bcc, list):
31 From = ("%s Support" % api.config.PLC_NAME,
32 api.config.PLC_MAIL_SUPPORT_ADDRESS)
34 # Create a MIME-encoded UTF-8 message
35 msg = MIMEText(Body.encode(api.encoding), _charset = api.encoding)
37 # Unicode subject headers are automatically encoded correctly
38 msg['Subject'] = Subject
40 def encode_addresses(addresses, header_name = None):
42 Unicode address headers are automatically encoded by
43 email.Header, but not correctly. The correct way is to put the
44 textual name inside quotes and the address inside brackets:
46 To: "=?utf-8?b?encoded" <recipient@domain>
48 Each address in addrs may be a tuple of (name, address) or
49 just an address. Returns a tuple of (header, addrlist)
50 representing the encoded header text and the list of plain
57 for addr in addresses:
58 if isinstance(addr, tuple):
61 name = name.encode('ascii')
62 header.append('%s <%s>' % (name, addr))
64 h = Header(name, charset = api.encoding, header_name = header_name)
65 header.append('"%s" <%s>' % (h.encode(), addr))
70 return (", ".join(header), addrs)
72 (msg['From'], from_addrs) = encode_addresses([From], 'From')
73 (msg['To'], to_addrs) = encode_addresses(To, 'To')
76 (msg['Cc'], cc_addrs) = encode_addresses(Cc, 'Cc')
80 (unused, bcc_addrs) = encode_addresses(Bcc, 'Bcc')
83 # Needed to pass some spam filters
84 msg['Reply-To'] = msg['From']
85 msg['X-Mailer'] = "Python/" + sys.version.split(" ")[0]
87 if not api.config.PLC_MAIL_ENABLED:
88 logger.info("PLC_MAIL_ENABLED not set")
89 logger.info("From: %(From)s, To: %(To)s, Subject: %(Subject)s" % msg)
94 rejected = s.sendmail(from_addrs[0], to_addrs, msg.as_string(), rcpt_options = ["NOTIFY=NEVER"])
98 raise PLCAPIError("Error sending message to " + ", ".join(list(rejected.keys())))