https://github.com/python/cpython/commit/f46a17b19ff535591b75f5f3df5b48f7b4f939b4
commit: f46a17b19ff535591b75f5f3df5b48f7b4f939b4
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: bitdancer <[email protected]>
date: 2026-04-08T18:14:15-04:00
summary:

[3.14] gh-70039: smtplib: store the server name in ._host in .connect() 
(GH-115259) (#148273)

Original patch by gigaplastik, extended with a few more tests.

Addresses gh-70039 and bpo-25852: failure of starttls if connect is called 
explicitly.
(cherry picked from commit 442f83a5ea1b4d334befd231a79c40d6ff41a0bd)

Co-authored-by: nmartensen <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-02-10-21-25-22.gh-issue-70039.6wvcAP.rst
M Lib/smtplib.py
M Lib/test/test_smtpnet.py

diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index 72093f7f8b0f2d..4cfc2338d99c67 100644
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -251,7 +251,6 @@ def __init__(self, host='', port=0, local_hostname=None,
         will be used.
 
         """
-        self._host = host
         self.timeout = timeout
         self.esmtp_features = {}
         self.command_encoding = 'ascii'
@@ -342,6 +341,7 @@ def connect(self, host='localhost', port=0, 
source_address=None):
                     port = int(port)
                 except ValueError:
                     raise OSError("nonnumeric port")
+        self._host = host
         if not port:
             port = self.default_port
         sys.audit("smtplib.connect", self, host, port)
diff --git a/Lib/test/test_smtpnet.py b/Lib/test/test_smtpnet.py
index d765746987bc4b..861e7e6a725c40 100644
--- a/Lib/test/test_smtpnet.py
+++ b/Lib/test/test_smtpnet.py
@@ -45,6 +45,59 @@ def test_connect_starttls(self):
             server.ehlo()
             server.quit()
 
+    def test_connect_host_port_starttls(self):
+        support.get_attribute(smtplib, 'SMTP_SSL')
+        context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+        context.check_hostname = False
+        context.verify_mode = ssl.CERT_NONE
+        with socket_helper.transient_internet(self.testServer):
+            server = smtplib.SMTP(f'{self.testServer}:{self.remotePort}')
+            try:
+                server.starttls(context=context)
+            except smtplib.SMTPException as e:
+                if e.args[0] == 'STARTTLS extension not supported by server.':
+                    unittest.skip(e.args[0])
+                else:
+                    raise
+            server.ehlo()
+            server.quit()
+
+    def test_explicit_connect_starttls(self):
+        support.get_attribute(smtplib, 'SMTP_SSL')
+        context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+        context.check_hostname = False
+        context.verify_mode = ssl.CERT_NONE
+        with socket_helper.transient_internet(self.testServer):
+            server = smtplib.SMTP()
+            server.connect(self.testServer, self.remotePort)
+            try:
+                server.starttls(context=context)
+            except smtplib.SMTPException as e:
+                if e.args[0] == 'STARTTLS extension not supported by server.':
+                    unittest.skip(e.args[0])
+                else:
+                    raise
+            server.ehlo()
+            server.quit()
+
+    def test_explicit_connect_host_port_starttls(self):
+        support.get_attribute(smtplib, 'SMTP_SSL')
+        context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+        context.check_hostname = False
+        context.verify_mode = ssl.CERT_NONE
+        with socket_helper.transient_internet(self.testServer):
+            server = smtplib.SMTP()
+            server.connect(f'{self.testServer}:{self.remotePort}')
+            try:
+                server.starttls(context=context)
+            except smtplib.SMTPException as e:
+                if e.args[0] == 'STARTTLS extension not supported by server.':
+                    unittest.skip(e.args[0])
+                else:
+                    raise
+            server.ehlo()
+            server.quit()
+
 
 class SmtpSSLTest(unittest.TestCase):
     testServer = SMTP_TEST_SERVER
diff --git 
a/Misc/NEWS.d/next/Library/2024-02-10-21-25-22.gh-issue-70039.6wvcAP.rst 
b/Misc/NEWS.d/next/Library/2024-02-10-21-25-22.gh-issue-70039.6wvcAP.rst
new file mode 100644
index 00000000000000..8bb2cd188e89fa
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-02-10-21-25-22.gh-issue-70039.6wvcAP.rst
@@ -0,0 +1 @@
+Fixed bug where :meth:`smtplib.SMTP.starttls` could fail if 
:meth:`smtplib.SMTP.connect` is called explicitly rather than implicitly.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to