-import os
+"""
+sendmail.py - Send email messages
+"""
+
+# import os
import sys
-import pprint
+# import pprint
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP
from PLC.Logger import logger
-from PLC.Faults import *
+from PLC.Faults import PLCAPIError
+# from PLC.Faults import *
+
-def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None):
+def sendmail(api, To, Subject, Body, From=None, Cc=None, Bcc=None):
"""
Uses sendmail (must be installed and running locally) to send a
message to the specified recipients. If the API is running under
api.config.PLC_MAIL_SUPPORT_ADDRESS)
# Create a MIME-encoded UTF-8 message
- msg = MIMEText(Body.encode(api.encoding), _charset = api.encoding)
+ msg = MIMEText(Body)
# Unicode subject headers are automatically encoded correctly
msg['Subject'] = Subject
- def encode_addresses(addresses, header_name = None):
+ def encode_addresses(addresses, header_name=None):
"""
Unicode address headers are automatically encoded by
email.Header, but not correctly. The correct way is to put the
for addr in addresses:
if isinstance(addr, tuple):
(name, addr) = addr
- try:
- name = name.encode('ascii')
- header.append('%s <%s>' % (name, addr))
- except:
- h = Header(name, charset = api.encoding, header_name = header_name)
- header.append('"%s" <%s>' % (h.encode(), addr))
+ h = Header(name, charset=api.encoding, header_name=header_name)
+ header.append('"%s" <%s>' % (h, addr))
else:
header.append(addr)
addrs.append(addr)
to_addrs += cc_addrs
if Bcc is not None:
- (unused, bcc_addrs) = encode_addresses(Bcc, 'Bcc')
+ (_, bcc_addrs) = encode_addresses(Bcc, 'Bcc')
to_addrs += bcc_addrs
# Needed to pass some spam filters
s = SMTP()
s.connect()
- rejected = s.sendmail(from_addrs[0], to_addrs, msg.as_string(), rcpt_options = ["NOTIFY=NEVER"])
- s.close()
+ rejected = s.sendmail(
+ from_addrs[0], to_addrs,
+ msg.as_string(), rcpt_options=["NOTIFY=NEVER"])
+ s.quit()
if rejected:
- raise PLCAPIError("Error sending message to " + ", ".join(list(rejected.keys())))
+ raise PLCAPIError("Error sending message to "
+ + ", ".join(list(rejected.keys())))