]> granicus.if.org Git - python/commitdiff
logging: Added optional 'secure' parameter to SMTPHandler.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Sun, 6 Dec 2009 17:57:11 +0000 (17:57 +0000)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Sun, 6 Dec 2009 17:57:11 +0000 (17:57 +0000)
Lib/logging/handlers.py
Misc/NEWS

index 2437c341aff6077a449953f206e1bba00c46caaf..d722e21cf1d6143afa16aee3bfb0b23fb9d44f7a 100644 (file)
@@ -803,7 +803,8 @@ class SMTPHandler(logging.Handler):
     """
     A handler class which sends an SMTP email for each logging event.
     """
-    def __init__(self, mailhost, fromaddr, toaddrs, subject, credentials=None):
+    def __init__(self, mailhost, fromaddr, toaddrs, subject,
+                 credentials=None, secure=False):
         """
         Initialize the handler.
 
@@ -811,7 +812,9 @@ class SMTPHandler(logging.Handler):
         line of the email. To specify a non-standard SMTP port, use the
         (host, port) tuple format for the mailhost argument. To specify
         authentication credentials, supply a (username, password) tuple
-        for the credentials argument.
+        for the credentials argument.  To specify the use of a secure
+        protocol (TLS), pass in True for the secure argument. This will
+        only be used when authentication credentials are supplied.
         """
         logging.Handler.__init__(self)
         if isinstance(mailhost, tuple):
@@ -827,6 +830,7 @@ class SMTPHandler(logging.Handler):
             toaddrs = [toaddrs]
         self.toaddrs = toaddrs
         self.subject = subject
+        self.secure = secure
 
     def getSubject(self, record):
         """
@@ -878,6 +882,10 @@ class SMTPHandler(logging.Handler):
                             self.getSubject(record),
                             formatdate(), msg)
             if self.username:
+                if self.secure:
+                    smtp.ehlo()
+                    smtp.starttls()
+                    smtp.ehlo()
                 smtp.login(self.username, self.password)
             smtp.sendmail(self.fromaddr, self.toaddrs, msg)
             smtp.quit()
index 0f524a52bac8e285f3c425cfbc3a2c3e3545c21a..2f762389ff02d0e54da97e592708a4fd96e77d98 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -154,6 +154,9 @@ C-API
 Library
 -------
 
+- logging: Added optional `secure` parameter to SMTPHandler, to enable use of
+  TLS with authentication credentials.
+
 - Issue #1923: Fixed the removal of meaningful spaces when PKG-INFO is 
   generated in Distutils. Patch by Stephen Emslie.