Skip to content

gh-127712: Fix secure argument of logging.handlers.SMTPHandler #127726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Lib/logging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,8 @@ def __init__(self, mailhost, fromaddr, toaddrs, subject,
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).
"""
Expand Down Expand Up @@ -1093,8 +1094,23 @@ def emit(self, record):
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of the ``secure`` argument of :class:`logging.handlers.SMTPHandler`.
Loading