-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-73065: Add Date header if missing in smtplib send_message #136850
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
Yoav11
wants to merge
15
commits into
python:main
Choose a base branch
from
Yoav11:bpo-28879
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.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6cfbdc6
bpo-28879: add Date header to send_message as per RFC5322
baf5025
rebase
ynir3 5ffc098
add patch
ynir3 4c2353c
update blurb
ynir3 c9e2af2
Merge branch 'main' into bpo-28879
Yoav11 a9cd436
Merge branch 'main' into bpo-28879
Yoav11 a4bc9c7
update rst
ynir3 f960ab2
update rst
ynir3 0e6ec22
update tests to not patch date
ynir3 be69865
reorder assertions for readability
ynir3 3a8abdd
.
ynir3 00c870a
use support.run_with_tz
ynir3 6a5188e
remove empty line
ynir3 bcfe9d2
use 02 syntax
ynir3 a9da40e
Merge branch 'main' into bpo-28879
Yoav11 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
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 |
---|---|---|
|
@@ -1467,6 +1467,7 @@ def test_send_unicode_with_SMTPUTF8_via_low_level_API(self): | |
self.assertIn('SMTPUTF8', self.serv.last_mail_options) | ||
self.assertEqual(self.serv.last_rcpt_options, []) | ||
|
||
@support.run_with_tz('UTC-02') | ||
def test_send_message_uses_smtputf8_if_addrs_non_ascii(self): | ||
msg = EmailMessage() | ||
msg['From'] = "Páolo <főo@bar.com>" | ||
|
@@ -1477,24 +1478,35 @@ def test_send_message_uses_smtputf8_if_addrs_non_ascii(self): | |
msg.set_content("oh là là, know what I mean, know what I mean?\n\n") | ||
# XXX smtpd converts received /r/n to /n, so we can't easily test that | ||
# we are successfully sending /r/n :(. | ||
smtp = smtplib.SMTP( | ||
HOST, self.port, local_hostname='localhost', | ||
timeout=support.LOOPBACK_TIMEOUT) | ||
self.addCleanup(smtp.close) | ||
self.assertEqual(smtp.send_message(msg), {}) | ||
|
||
last_message = self.serv.last_message.decode() | ||
date = email.message_from_string(last_message)['Date'] | ||
# asserts RFC 5322 section 3.3 4th Paragraph | ||
self.assertEqual( | ||
email.utils.parsedate_to_datetime(date).tzname(), | ||
"UTC+02:00" | ||
) | ||
|
||
expected = textwrap.dedent("""\ | ||
From: Páolo <főo@bar.com> | ||
To: Dinsdale | ||
Subject: Nudge nudge, wink, wink \u1F609 | ||
Content-Type: text/plain; charset="utf-8" | ||
Content-Transfer-Encoding: 8bit | ||
MIME-Version: 1.0 | ||
Date: {} | ||
|
||
oh là là, know what I mean, know what I mean? | ||
""") | ||
smtp = smtplib.SMTP( | ||
HOST, self.port, local_hostname='localhost', | ||
timeout=support.LOOPBACK_TIMEOUT) | ||
self.addCleanup(smtp.close) | ||
self.assertEqual(smtp.send_message(msg), {}) | ||
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 am moving these assertions further up so that I can extract the date from the message in order to build the |
||
""".format(date)) | ||
|
||
self.assertEqual(self.serv.last_mailfrom, 'főo@bar.com') | ||
self.assertEqual(self.serv.last_rcpttos, ['Dinsdale']) | ||
self.assertEqual(self.serv.last_message.decode(), expected) | ||
self.assertEqual(last_message, expected) | ||
self.assertIn('BODY=8BITMIME', self.serv.last_mail_options) | ||
self.assertIn('SMTPUTF8', self.serv.last_mail_options) | ||
self.assertEqual(self.serv.last_rcpt_options, []) | ||
|
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2025-07-20-09-51-25.bpo-28879.SLDgcy.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,2 @@ | ||
Fix ``smtplib.send_message`` to add a ``Date`` header if it is missing as | ||
per :rfc:`5322`. |
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.
We need to make it work in any timezone
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.
do you have a suggestion for a specific timezone ? my concern with using something more generic like
australia
/london
etc. is that (although rare) there is some chance that the timezone name or DST conventions change over time which would break the test. As i'm only trying to assert that the user's local was respected in this test, i felt like using something likeUTC+02
was less prone to flakiness in the future. But maybe my concern is unfounded, I don't have a ton of expertise with date times :p