3ce0f69cdd8025889301afff6da128ae4eb6fe7b
[plcapi.git] / PLC / sendmail.py
1 # $Id$
2 import os
3 import sys
4 import pprint
5 from types import StringTypes
6 from email.MIMEText import MIMEText
7 from email.Header import Header
8 from smtplib import SMTP
9
10 from PLC.Debug import log
11 from PLC.Faults import *
12
13 def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None):
14     """
15     Uses sendmail (must be installed and running locally) to send a
16     message to the specified recipients. If the API is running under
17     mod_python, the apache user must be listed in e.g.,
18     /etc/mail/trusted-users.
19
20     To, Cc, and Bcc may be addresses or lists of addresses. Each
21     address may be either a plain text address or a tuple of (name,
22     address).
23     """
24
25     # Fix up defaults
26     if not isinstance(To, list):
27         To = [To]
28     if Cc is not None and not isinstance(Cc, list):
29         Cc = [Cc]
30     if Bcc is not None and not isinstance(Bcc, list):
31         Bcc = [Bcc]
32     if From is None:
33         From = ("%s Support" % api.config.PLC_NAME,
34                 api.config.PLC_MAIL_SUPPORT_ADDRESS)
35
36     # Create a MIME-encoded UTF-8 message
37     msg = MIMEText(Body.encode(api.encoding), _charset = api.encoding)
38
39     # Unicode subject headers are automatically encoded correctly
40     msg['Subject'] = Subject
41
42     def encode_addresses(addresses, header_name = None):
43         """
44         Unicode address headers are automatically encoded by
45         email.Header, but not correctly. The correct way is to put the
46         textual name inside quotes and the address inside brackets:
47
48         To: "=?utf-8?b?encoded" <recipient@domain>
49
50         Each address in addrs may be a tuple of (name, address) or
51         just an address. Returns a tuple of (header, addrlist)
52         representing the encoded header text and the list of plain
53         text addresses.
54         """
55
56         header = []
57         addrs = []
58
59         for addr in addresses:
60             if isinstance(addr, tuple):
61                 (name, addr) = addr
62                 try:
63                     name = name.encode('ascii')
64                     header.append('%s <%s>' % (name, addr))
65                 except:
66                     h = Header(name, charset = api.encoding, header_name = header_name)
67                     header.append('"%s" <%s>' % (h.encode(), addr))
68             else:
69                 header.append(addr)
70             addrs.append(addr)
71
72         return (", ".join(header), addrs)
73
74     (msg['From'], from_addrs) = encode_addresses([From], 'From')
75     (msg['To'], to_addrs) = encode_addresses(To, 'To')
76
77     if Cc is not None:
78         (msg['Cc'], cc_addrs) = encode_addresses(Cc, 'Cc')
79         to_addrs += cc_addrs
80
81     if Bcc is not None:
82         (unused, bcc_addrs) = encode_addresses(Bcc, 'Bcc')
83         to_addrs += bcc_addrs
84
85     # Needed to pass some spam filters
86     msg['Reply-To'] = msg['From']
87     msg['X-Mailer'] = "Python/" + sys.version.split(" ")[0]
88
89     if not api.config.PLC_MAIL_ENABLED:
90         print >> log, "From: %(From)s, To: %(To)s, Subject: %(Subject)s" % msg
91         return
92
93     s = SMTP()
94     s.connect()
95     rejected = s.sendmail(from_addrs[0], to_addrs, msg.as_string(), rcpt_options = ["NOTIFY=NEVER"])
96     s.close()
97
98     if rejected:
99         raise PLCAPIError, "Error sending message to " + ", ".join(rejected.keys())