Repository navigation
gh-47711: Use connected source address for SMTP address literals #159166
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
Open
ishu7w
wants to merge
1
commit into
python:main
Choose a base branch
from
ishu7w:fix-smtp-ipv6-ehlo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,11 +257,7 @@ def __init__(self, host='', port=0, local_hostname=None, | |
| self.source_address = source_address | ||
| self._auth_challenge_count = 0 | ||
|
|
||
| if host: | ||
| (code, msg) = self.connect(host, port) | ||
| if code != 220: | ||
| self.close() | ||
| raise SMTPConnectError(code, msg) | ||
| self._find_local_hostname = False | ||
| if local_hostname is not None: | ||
| self.local_hostname = local_hostname | ||
| else: | ||
|
|
@@ -273,13 +269,20 @@ def __init__(self, host='', port=0, local_hostname=None, | |
| self.local_hostname = fqdn | ||
| else: | ||
| # We can't find an fqdn hostname, so use a domain literal | ||
| self._find_local_hostname = True | ||
| addr = '127.0.0.1' | ||
| try: | ||
| addr = socket.gethostbyname(socket.gethostname()) | ||
| except socket.gaierror: | ||
| pass | ||
| self.local_hostname = '[%s]' % addr | ||
|
|
||
| if host: | ||
| (code, msg) = self.connect(host, port) | ||
| if code != 220: | ||
| self.close() | ||
| raise SMTPConnectError(code, msg) | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
|
|
@@ -348,6 +351,12 @@ def connect(self, host='localhost', port=0, source_address=None): | |
| self.sock = self._get_socket(host, port, self.timeout) | ||
| self.file = None | ||
| (code, msg) = self.getreply() | ||
| if code == 220 and self._find_local_hostname: | ||
| addr = self.sock.getsockname()[0] | ||
| if self.sock.family == socket.AF_INET6: | ||
| # RFC 5321 section 4.1.3 requires the IPv6 tag and excludes scope IDs. | ||
| addr = f"IPv6:{addr.split('%', 1)[0]}" | ||
| self.local_hostname = '[%s]' % addr | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be clearer to use an f-string here.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed the IPv6 literal construction to an f-string. |
||
| if self.debuglevel > 0: | ||
| self._print_debug('connect:', repr(msg)) | ||
| return (code, msg) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-10-11-00-00-00.gh-issue-47711.smtp-ipv6.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| When no fully qualified hostname is available, :mod:`smtplib` now uses the | ||
| connected socket's local address for the HELO/EHLO address literal. Previously, | ||
| IPv6 was not handled correctly. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can see, the second part of this inner else is now code that doesn't have any effect, since local_hostname then gets reset unconditionally in connect. I suggest the following:
And then of course changing
_use_socket_hostnameto_find_local_hostnameinconnect. What do you think? The tests, including the new ones, still pass.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the flag to _find_local_hostname and now defer hostname fallback address selection to connect, following your suggested flow.