Kaydet (Commit) 3f445f79 authored tarafından Vinay Sajip's avatar Vinay Sajip

Closes #25411: Merged fix from 3.4.

...@@ -965,24 +965,26 @@ class SMTPHandler(logging.Handler): ...@@ -965,24 +965,26 @@ class SMTPHandler(logging.Handler):
""" """
try: try:
import smtplib import smtplib
from email.utils import formatdate from email.message import EmailMessage
import email.utils
port = self.mailport port = self.mailport
if not port: if not port:
port = smtplib.SMTP_PORT port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout) smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
msg = self.format(record) msg = EmailMessage()
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % ( msg['From'] = self.fromaddr
self.fromaddr, msg['To'] = ','.join(self.toaddrs)
",".join(self.toaddrs), msg['Subject'] = self.getSubject(record)
self.getSubject(record), msg['Date'] = email.utils.localtime()
formatdate(), msg) msg.set_content(self.format(record))
if self.username: if self.username:
if self.secure is not None: if self.secure is not None:
smtp.ehlo() smtp.ehlo()
smtp.starttls(*self.secure) smtp.starttls(*self.secure)
smtp.ehlo() smtp.ehlo()
smtp.login(self.username, self.password) smtp.login(self.username, self.password)
smtp.sendmail(self.fromaddr, self.toaddrs, msg) smtp.send_message(msg)
smtp.quit() smtp.quit()
except Exception: except Exception:
self.handleError(record) self.handleError(record)
......
...@@ -930,7 +930,7 @@ class SMTPHandlerTest(BaseTest): ...@@ -930,7 +930,7 @@ class SMTPHandlerTest(BaseTest):
timeout=self.TIMEOUT) timeout=self.TIMEOUT)
self.assertEqual(h.toaddrs, ['you']) self.assertEqual(h.toaddrs, ['you'])
self.messages = [] self.messages = []
r = logging.makeLogRecord({'msg': 'Hello'}) r = logging.makeLogRecord({'msg': 'Hello \u2713'})
self.handled = threading.Event() self.handled = threading.Event()
h.handle(r) h.handle(r)
self.handled.wait(self.TIMEOUT) # 14314: don't wait forever self.handled.wait(self.TIMEOUT) # 14314: don't wait forever
...@@ -941,7 +941,7 @@ class SMTPHandlerTest(BaseTest): ...@@ -941,7 +941,7 @@ class SMTPHandlerTest(BaseTest):
self.assertEqual(mailfrom, 'me') self.assertEqual(mailfrom, 'me')
self.assertEqual(rcpttos, ['you']) self.assertEqual(rcpttos, ['you'])
self.assertIn('\nSubject: Log\n', data) self.assertIn('\nSubject: Log\n', data)
self.assertTrue(data.endswith('\n\nHello')) self.assertTrue(data.endswith('\n\nHello \u2713'))
h.close() h.close()
def process_message(self, *args): def process_message(self, *args):
......
...@@ -45,6 +45,9 @@ Core and Builtins ...@@ -45,6 +45,9 @@ Core and Builtins
Library Library
------- -------
- Issue #25411: Improved Unicode support in SMTPHandler through better use of
the email package. Thanks to user simon04 for the patch.
- Issue #25407: Remove mentions of the formatter module being removed in - Issue #25407: Remove mentions of the formatter module being removed in
Python 3.6. Python 3.6.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment