Skip to content

Commit fd665d5

Browse files
[3.13] gh-127712: Fix secure argument of logging.handlers.SMTPHandler (GH-127726) (GH-129955)
(cherry picked from commit d7672e5)
1 parent 7df30bb commit fd665d5

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
@@ -1040,7 +1040,8 @@ def __init__(self, mailhost, fromaddr, toaddrs, subject,
10401040
only be used when authentication credentials are supplied. The tuple
10411041
will be either an empty tuple, or a single-value tuple with the name
10421042
of a keyfile, or a 2-value tuple with the names of the keyfile and
1043-
certificate file. (This tuple is passed to the `starttls` method).
1043+
certificate file. (This tuple is passed to the
1044+
`ssl.SSLContext.load_cert_chain` method).
10441045
A timeout in seconds can be specified for the SMTP connection (the
10451046
default is one second).
10461047
"""
@@ -1093,8 +1094,23 @@ def emit(self, record):
10931094
msg.set_content(self.format(record))
10941095
if self.username:
10951096
if self.secure is not None:
1097+
import ssl
1098+
1099+
try:
1100+
keyfile = self.secure[0]
1101+
except IndexError:
1102+
keyfile = None
1103+
1104+
try:
1105+
certfile = self.secure[1]
1106+
except IndexError:
1107+
certfile = None
1108+
1109+
context = ssl._create_stdlib_context(
1110+
certfile=certfile, keyfile=keyfile
1111+
)
10961112
smtp.ehlo()
1097-
smtp.starttls(*self.secure)
1113+
smtp.starttls(context=context)
10981114
smtp.ehlo()
10991115
smtp.login(self.username, self.password)
11001116
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)