Skip to content

Commit 2fb480e

Browse files
committed
issue2551350 - Python changes for 3.12 with roundup 2.3.0 mailer.py
Fix due to change in smtplib.SMTP.starttls() signature. As of 3.3 it can use an optional ssl context argument for certificates/keys. In 3.12 it dropped legacy support for specifing cert/key files as arguments and requires a context. I modified Andrew's original patch to initialize SSLContext with ssl.PROTOCOL_TLS_CLIENT. If there is a cert file specified, enable check_hostname - verify that the cert supplied by the server matches the hostname we supplied. If there is no cert file call load_default_certs() Also opened issue2551351 to look into more SMTP ssmtp tightening. We also should have an option in Roundup to use TLS/SSL (smtps) without using starttls. Note that this code is untested by the test suite due to the need to setup an SMTP server with STARTTLS support. issue2551351 has some notes on this.
1 parent ab3a6f5 commit 2fb480e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ Fixed:
145145
- issue2551350 - Python changes for 3.12 with roundup 2.3.0. Fixes for
146146
cgitb.py crash due to pydoc.html.header() signature change. (Patch
147147
by Andrew (kragacles), applied John Rouillard)
148+
- issue2551350 - Python changes for 3.12 with roundup 2.3.0. Fixes for
149+
mailer.py crash due to change in starttls signature change. (Patch
150+
by Andrew (kragacles), modified and applied John Rouillard)
148151

149152
Features:
150153

roundup/mailer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import smtplib
88
import socket
9+
import ssl
910
import sys
1011
import time
1112
import traceback
@@ -312,8 +313,18 @@ def __init__(self, config):
312313
# start the TLS if requested
313314
if config["MAIL_TLS"]:
314315
self.ehlo()
315-
self.starttls(config["MAIL_TLS_KEYFILE"],
316-
config["MAIL_TLS_CERTFILE"])
316+
if sys.version_info[0:2] >= (3, 6):
317+
sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
318+
if config["MAIL_TLS_CERTFILE"]:
319+
sslctx.load_cert_chain(config["MAIL_TLS_CERTFILE"],
320+
keyfile=config["MAIL_TLS_KEYFILE"])
321+
sslctx.check_hostname = True
322+
else:
323+
sslctx.load_default_certs()
324+
self.starttls(context=sslctx)
325+
else:
326+
self.starttls(config["MAIL_TLS_KEYFILE"],
327+
config["MAIL_TLS_CERTFILE"])
317328

318329
# ok, now do we also need to log in?
319330
mailuser = config["MAIL_USERNAME"]

0 commit comments

Comments
 (0)