-
-
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 1 commit
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
Next
Next commit
bpo-28879: add Date header to send_message as per RFC5322
- Loading branch information
commit 6cfbdc6cfbbdbec9ba4119c3152a78e404072118
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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
from test.support import hashlib_helper | ||
from test.support import socket_helper | ||
from test.support import threading_helper | ||
from unittest.mock import Mock | ||
from unittest.mock import Mock, patch | ||
|
||
HOST = socket_helper.HOST | ||
|
||
|
@@ -1342,36 +1342,40 @@ def test_send_unicode_with_SMTPUTF8_via_low_level_API(self): | |
self.assertEqual(self.serv.last_rcpt_options, []) | ||
|
||
def test_send_message_uses_smtputf8_if_addrs_non_ascii(self): | ||
msg = EmailMessage() | ||
msg['From'] = "Páolo <főo@bar.com>" | ||
msg['To'] = 'Dinsdale' | ||
msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' | ||
# XXX I don't know why I need two \n's here, but this is an existing | ||
# bug (if it is one) and not a problem with the new functionality. | ||
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 :(. | ||
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 | ||
|
||
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 |
||
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.assertIn('BODY=8BITMIME', self.serv.last_mail_options) | ||
self.assertIn('SMTPUTF8', self.serv.last_mail_options) | ||
self.assertEqual(self.serv.last_rcpt_options, []) | ||
expected_date = "Thu, 19 Mar 2020 00:59:43 -0000" | ||
with patch("email.utils.formatdate") as date_mock: | ||
date_mock.return_value = expected_date | ||
msg = EmailMessage() | ||
msg['From'] = "Páolo <főo@bar.com>" | ||
msg['To'] = 'Dinsdale' | ||
msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' | ||
# XXX I don't know why I need two \n's here, but this is an existing | ||
# bug (if it is one) and not a problem with the new functionality. | ||
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 :(. | ||
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? | ||
""".format(expected_date)) | ||
smtp = smtplib.SMTP( | ||
HOST, self.port, local_hostname='localhost', | ||
timeout=support.LOOPBACK_TIMEOUT) | ||
self.addCleanup(smtp.close) | ||
self.assertEqual(smtp.send_message(msg), {}) | ||
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.assertIn('BODY=8BITMIME', self.serv.last_mail_options) | ||
self.assertIn('SMTPUTF8', self.serv.last_mail_options) | ||
self.assertEqual(self.serv.last_rcpt_options, []) | ||
|
||
|
||
EXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='') | ||
|
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2018-01-13-15-52-00.bpo-28879.aW2gj0.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 | ||
RFC5322. |
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.
Uh oh!
There was an error while loading. Please reload this page.