Skip to content

Commit 2db4a9d

Browse files
[3.12] gh-127712: Fix secure argument of logging.handlers.SMTPHandler (GH-127726) (GH-129956)
(cherry picked from commit d7672e5)
1 parent 08a9b7c commit 2db4a9d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Lib/logging/handlers.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,8 @@ def __init__(self, mailhost, fromaddr, toaddrs, subject,
10311031
only be used when authentication credentials are supplied. The tuple
10321032
will be either an empty tuple, or a single-value tuple with the name
10331033
of a keyfile, or a 2-value tuple with the names of the keyfile and
1034-
certificate file. (This tuple is passed to the `starttls` method).
1034+
certificate file. (This tuple is passed to the
1035+
`ssl.SSLContext.load_cert_chain` method).
10351036
A timeout in seconds can be specified for the SMTP connection (the
10361037
default is one second).
10371038
"""
@@ -1084,8 +1085,23 @@ def emit(self, record):
10841085
msg.set_content(self.format(record))
10851086
if self.username:
10861087
if self.secure is not None:
1088+
import ssl
1089+
1090+
try:
1091+
keyfile = self.secure[0]
1092+
except IndexError:
1093+
keyfile = None
1094+
1095+
try:
1096+
certfile = self.secure[1]
1097+
except IndexError:
1098+
certfile = None
1099+
1100+
context = ssl._create_stdlib_context(
1101+
certfile=certfile, keyfile=keyfile
1102+
)
10871103
smtp.ehlo()
1088-
smtp.starttls(*self.secure)
1104+
smtp.starttls(context=context)
10891105
smtp.ehlo()
10901106
smtp.login(self.username, self.password)
10911107
smtp.send_message(msg)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix handling of the ``secure`` argument of :class:`logging.handlers.SMTPHandler`.

0 commit comments

Comments
 (0)