Skip to content

Commit 277640a

Browse files
committed
Closes #25411: Improved Unicode support in SMTPHandler.
1 parent 4de9dae commit 277640a

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

Lib/logging/handlers.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
1818
Additional handlers for the logging package for Python. The core package is
1919
based on PEP 282 and comments thereto in comp.lang.python.
2020
21-
Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved.
2222
2323
To use, simply 'import logging.handlers' and log away!
2424
"""
@@ -965,24 +965,26 @@ def emit(self, record):
965965
"""
966966
try:
967967
import smtplib
968-
from email.utils import formatdate
968+
from email.message import EmailMessage
969+
import email.utils
970+
969971
port = self.mailport
970972
if not port:
971973
port = smtplib.SMTP_PORT
972974
smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
973-
msg = self.format(record)
974-
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
975-
self.fromaddr,
976-
",".join(self.toaddrs),
977-
self.getSubject(record),
978-
formatdate(), msg)
975+
msg = EmailMessage()
976+
msg['From'] = self.fromaddr
977+
msg['To'] = ','.join(self.toaddrs)
978+
msg['Subject'] = self.getSubject(record)
979+
msg['Date'] = email.utils.localtime()
980+
msg.set_content(self.format(record))
979981
if self.username:
980982
if self.secure is not None:
981983
smtp.ehlo()
982984
smtp.starttls(*self.secure)
983985
smtp.ehlo()
984986
smtp.login(self.username, self.password)
985-
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
987+
smtp.send_message(msg)
986988
smtp.quit()
987989
except Exception:
988990
self.handleError(record)

Lib/test/test_logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ def test_basic(self):
935935
timeout=self.TIMEOUT)
936936
self.assertEqual(h.toaddrs, ['you'])
937937
self.messages = []
938-
r = logging.makeLogRecord({'msg': 'Hello'})
938+
r = logging.makeLogRecord({'msg': 'Hello \u2713'})
939939
self.handled = threading.Event()
940940
h.handle(r)
941941
self.handled.wait(self.TIMEOUT) # 14314: don't wait forever
@@ -946,7 +946,7 @@ def test_basic(self):
946946
self.assertEqual(mailfrom, 'me')
947947
self.assertEqual(rcpttos, ['you'])
948948
self.assertIn('\nSubject: Log\n', data)
949-
self.assertTrue(data.endswith('\n\nHello'))
949+
self.assertTrue(data.endswith('\n\nHello \u2713'))
950950
h.close()
951951

952952
def process_message(self, *args):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Core and Builtins
9696
Library
9797
-------
9898

99+
- Issue #25411: Improved Unicode support in SMTPHandler through better use of
100+
the email package. Thanks to user simon04 for the patch.
101+
99102
- Issue #25380: Fixed protocol for the STACK_GLOBAL opcode in
100103
pickletools.opcodes.
101104

0 commit comments

Comments
 (0)