]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Closes #25411: Improved Unicode support in SMTPHandler.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Sat, 17 Oct 2015 15:13:10 +0000 (16:13 +0100)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Sat, 17 Oct 2015 15:13:10 +0000 (16:13 +0100)
Lib/logging/handlers.py
Lib/test/test_logging.py
Misc/NEWS

index fda809334b1f524d4da518b1f9ba5eadd24f25a6..13a8fb2de1ad2b519d4eec92bfac450207d25682 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
 # documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
 Additional handlers for the logging package for Python. The core package is
 based on PEP 282 and comments thereto in comp.lang.python.
 
-Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved.
 
 To use, simply 'import logging.handlers' and log away!
 """
@@ -965,24 +965,26 @@ class SMTPHandler(logging.Handler):
         """
         try:
             import smtplib
-            from email.utils import formatdate
+            from email.message import EmailMessage
+            import email.utils
+
             port = self.mailport
             if not port:
                 port = smtplib.SMTP_PORT
             smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
-            msg = self.format(record)
-            msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
-                            self.fromaddr,
-                            ",".join(self.toaddrs),
-                            self.getSubject(record),
-                            formatdate(), msg)
+            msg = EmailMessage()
+            msg['From'] = self.fromaddr
+            msg['To'] = ','.join(self.toaddrs)
+            msg['Subject'] = self.getSubject(record)
+            msg['Date'] = email.utils.localtime()
+            msg.set_content(self.format(record))
             if self.username:
                 if self.secure is not None:
                     smtp.ehlo()
                     smtp.starttls(*self.secure)
                     smtp.ehlo()
                 smtp.login(self.username, self.password)
-            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
+            smtp.send_message(msg)
             smtp.quit()
         except Exception:
             self.handleError(record)
index deba2e49fad267565002efadaef680643a854aee..b17f5e5f54864737f58d8c13df07de5bd1110da5 100644 (file)
@@ -935,7 +935,7 @@ class SMTPHandlerTest(BaseTest):
                                          timeout=self.TIMEOUT)
         self.assertEqual(h.toaddrs, ['you'])
         self.messages = []
-        r = logging.makeLogRecord({'msg': 'Hello'})
+        r = logging.makeLogRecord({'msg': 'Hello \u2713'})
         self.handled = threading.Event()
         h.handle(r)
         self.handled.wait(self.TIMEOUT)  # 14314: don't wait forever
@@ -946,7 +946,7 @@ class SMTPHandlerTest(BaseTest):
         self.assertEqual(mailfrom, 'me')
         self.assertEqual(rcpttos, ['you'])
         self.assertIn('\nSubject: Log\n', data)
-        self.assertTrue(data.endswith('\n\nHello'))
+        self.assertTrue(data.endswith('\n\nHello \u2713'))
         h.close()
 
     def process_message(self, *args):
index 23b6ebcc2a177cf16a97683ac7696274bbba884b..c6a4d3f9f598c5798ee996ca2a617ef5ab926f5d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -96,6 +96,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #25411: Improved Unicode support in SMTPHandler through better use of
+  the email package. Thanks to user simon04 for the patch.
+
 - Issue #25380: Fixed protocol for the STACK_GLOBAL opcode in
   pickletools.opcodes.