X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2Fsendmail.py;h=a215dc28020aa2fb3b8a0d348ce76781fe2e76f3;hb=e68365ae0a76902ee71d13a154ed5035b10e8109;hp=accb0bb1469180ac9671d81f65e2bb93b4b3a1a7;hpb=3b44c0228c26dc43d985185afc225caa5f48c1fb;p=plcapi.git diff --git a/PLC/sendmail.py b/PLC/sendmail.py index accb0bb..a215dc2 100644 --- a/PLC/sendmail.py +++ b/PLC/sendmail.py @@ -1,15 +1,20 @@ -import os +""" +sendmail.py - Send email messages +""" + +# import os import sys -import pprint -from types import StringTypes -from email.MIMEText import MIMEText -from email.Header import Header +# 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 @@ -30,15 +35,15 @@ def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None): Bcc = [Bcc] if From is None: From = ("%s Support" % api.config.PLC_NAME, - api.config.PLC_MAIL_SUPPORT_ADDRESS) + api.config.PLC_MAIL_FROM_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 @@ -58,12 +63,8 @@ def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None): 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) @@ -78,7 +79,7 @@ def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None): 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 @@ -92,8 +93,11 @@ def sendmail(api, To, Subject, Body, From = None, Cc = None, Bcc = None): 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())))