diff options
author | s-hamann <10639154+s-hamann@users.noreply.github.com> | 2025-02-10 12:34:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-10 12:34:27 (GMT) |
commit | d7672e5d5a7b9580a72dbe75d3a9e8840bcc604c (patch) | |
tree | 4f932c5b92fff1d245c3c4f1592b21c82ecd6db9 | |
parent | 94cd2e0ddeff83dee3254ca356d9e4396927d075 (diff) | |
download | cpython-d7672e5d5a7b9580a72dbe75d3a9e8840bcc604c.zip cpython-d7672e5d5a7b9580a72dbe75d3a9e8840bcc604c.tar.gz cpython-d7672e5d5a7b9580a72dbe75d3a9e8840bcc604c.tar.bz2 |
gh-127712: Fix `secure` argument of `logging.handlers.SMTPHandler` (GH-127726)
GH-127712: Fix `secure` argument of `logging.handlers.SMTPHandler`
Python 3.12 removed support for the `keyfile` and `certfile` parameters
in `smtplib.SMTP.starttls()`, requiring a `ssl.SSLContext` instead.
`SMTPHandler` now creates a context from the `secure` tuple and passes
that to `starttls`.
-rw-r--r-- | Lib/logging/handlers.py | 20 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2024-12-07-20-33-43.gh-issue-127712.Uzsij4.rst | 1 |
2 files changed, 19 insertions, 2 deletions
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 017c9ab..9abadbf 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -1043,7 +1043,8 @@ class SMTPHandler(logging.Handler): only be used when authentication credentials are supplied. The tuple will be either an empty tuple, or a single-value tuple with the name of a keyfile, or a 2-value tuple with the names of the keyfile and - certificate file. (This tuple is passed to the `starttls` method). + certificate file. (This tuple is passed to the + `ssl.SSLContext.load_cert_chain` method). A timeout in seconds can be specified for the SMTP connection (the default is one second). """ @@ -1096,8 +1097,23 @@ class SMTPHandler(logging.Handler): msg.set_content(self.format(record)) if self.username: if self.secure is not None: + import ssl + + try: + keyfile = self.secure[0] + except IndexError: + keyfile = None + + try: + certfile = self.secure[1] + except IndexError: + certfile = None + + context = ssl._create_stdlib_context( + certfile=certfile, keyfile=keyfile + ) smtp.ehlo() - smtp.starttls(*self.secure) + smtp.starttls(context=context) smtp.ehlo() smtp.login(self.username, self.password) smtp.send_message(msg) diff --git a/Misc/NEWS.d/next/Library/2024-12-07-20-33-43.gh-issue-127712.Uzsij4.rst b/Misc/NEWS.d/next/Library/2024-12-07-20-33-43.gh-issue-127712.Uzsij4.rst new file mode 100644 index 0000000..40450cd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-07-20-33-43.gh-issue-127712.Uzsij4.rst @@ -0,0 +1 @@ +Fix handling of the ``secure`` argument of :class:`logging.handlers.SMTPHandler`. |