From 943ee6eeed35936cc91a1960a2f7fc5f3c7fdf35 Mon Sep 17 00:00:00 2001 From: lafe3402 Date: Mon, 10 Jul 2017 22:04:39 -0400 Subject: [PATCH 1/6] bpo-28879 : add date if missing in smtplib.send_message --- Doc/library/smtplib.rst | 4 +++- Lib/smtplib.py | 6 ++++++ Lib/test/test_smtplib.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index 86e769e6a1f8dc..d2422128f01b4d 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -496,7 +496,9 @@ An :class:`SMTP` instance has the following methods: specified in :rfc:`5322`\: *from_addr* is set to the :mailheader:`Sender` field if it is present, and otherwise to the :mailheader:`From` field. *to_addrs* combines the values (if any) of the :mailheader:`To`, - :mailheader:`Cc`, and :mailheader:`Bcc` fields from *msg*. If exactly one + :mailheader:`Cc`, and :mailheader:`Bcc` fields from *msg*. If there's no + *Date* header inside the message, ``send_message`` will add one to the data. + If exactly one set of :mailheader:`Resent-*` headers appear in the message, the regular headers are ignored and the :mailheader:`Resent-*` headers are used instead. If the message contains more than one set of :mailheader:`Resent-*` headers, diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 5e422b704ad4dc..eac4a9deac4b5d 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -928,6 +928,12 @@ def send_message(self, msg, from_addr=None, to_addrs=None, header_prefix = 'Resent-' else: raise ValueError("message has more than one 'Resent-' header block") + + # RFC 5322 section 3.6, 4th Paragraph + if msg.get('Date',None) is None: + msg['Date'] = email.utils.formatdate() + + if from_addr is None: # Prefer the sender field per RFC 2822:3.6.2. from_addr = (msg[header_prefix + 'Sender'] diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index 28539f360f5974..1222fb4e88b0ab 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -549,6 +549,36 @@ def testSendMessageMultipleResentRaises(self): smtp.send_message(m) smtp.close() + def testSendMessageAddDateIfMissing(self): + m = email.mime.text.MIMEText('A test message') + m['From'] = 'foo@bar.com' + m['To'] = 'John' + m['CC'] = 'Sally, Fred' + m['Bcc'] = 'John Root , "Dinsdale" ' + smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) + smtp.send_message(m) + # XXX (see comment in testSend) + time.sleep(0.01) + smtp.quit() + + self.client_evt.set() + self.serv_evt.wait() + self.output.flush() + # The Resent-Bcc headers are deleted before serialization. + del m['Bcc'] + del m['Resent-Bcc'] + # Add the X-Peer header that DebuggingServer adds + m['X-Peer'] = socket.gethostbyname('localhost') + mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) + self.assertEqual(self.output.getvalue(), mexpect) + debugout = smtpd.DEBUGSTREAM.getvalue() + current_moment = email.utils.formatdate() + Date = re.compile(''.join(("\\\\nDate: ",current_moment[:16])),re.MULTILINE) + self.assertRegex(debugout, Date) + + + + class NonConnectingTests(unittest.TestCase): def testNotConnected(self): From 9a8cdc2670427c61bb49b2575be019b0f8b4ae94 Mon Sep 17 00:00:00 2001 From: lafe3402 Date: Tue, 11 Jul 2017 08:04:11 -0400 Subject: [PATCH 2/6] bpo-28879 : add date if missing in smtplib.send_message part 2 Fix the whitespace and spacing --- Doc/library/smtplib.rst | 2 +- Lib/smtplib.py | 5 ++--- Lib/test/test_smtplib.py | 2 -- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst index d2422128f01b4d..32327c17f3d249 100644 --- a/Doc/library/smtplib.rst +++ b/Doc/library/smtplib.rst @@ -496,7 +496,7 @@ An :class:`SMTP` instance has the following methods: specified in :rfc:`5322`\: *from_addr* is set to the :mailheader:`Sender` field if it is present, and otherwise to the :mailheader:`From` field. *to_addrs* combines the values (if any) of the :mailheader:`To`, - :mailheader:`Cc`, and :mailheader:`Bcc` fields from *msg*. If there's no + :mailheader:`Cc`, and :mailheader:`Bcc` fields from *msg*. If there's no *Date* header inside the message, ``send_message`` will add one to the data. If exactly one set of :mailheader:`Resent-*` headers appear in the message, the regular diff --git a/Lib/smtplib.py b/Lib/smtplib.py index eac4a9deac4b5d..7174f6fd4e3593 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -928,12 +928,11 @@ def send_message(self, msg, from_addr=None, to_addrs=None, header_prefix = 'Resent-' else: raise ValueError("message has more than one 'Resent-' header block") - + # RFC 5322 section 3.6, 4th Paragraph if msg.get('Date',None) is None: msg['Date'] = email.utils.formatdate() - - + if from_addr is None: # Prefer the sender field per RFC 2822:3.6.2. from_addr = (msg[header_prefix + 'Sender'] diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index 1222fb4e88b0ab..087340ae7775a0 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -577,8 +577,6 @@ def testSendMessageAddDateIfMissing(self): self.assertRegex(debugout, Date) - - class NonConnectingTests(unittest.TestCase): def testNotConnected(self): From 0c436447833015f06f884125084f4e31e3ce1ad3 Mon Sep 17 00:00:00 2001 From: lafe3402 Date: Thu, 20 Jul 2017 13:29:08 -0400 Subject: [PATCH 3/6] bpo-28879 : Mock the date on the Test Case + Fix Typo Following the discussion on the bpo-28879, I've put space between commas and the variable to be compliant and mocked the "Date" call being made to the send_message function --- Lib/smtplib.py | 2 +- Lib/test/test_smtplib.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 7174f6fd4e3593..09cfc3c9bdaa85 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -930,7 +930,7 @@ def send_message(self, msg, from_addr=None, to_addrs=None, raise ValueError("message has more than one 'Resent-' header block") # RFC 5322 section 3.6, 4th Paragraph - if msg.get('Date',None) is None: + if msg.get('Date', None) is None: msg['Date'] = email.utils.formatdate() if from_addr is None: diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index 087340ae7775a0..62a43536841617 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -17,6 +17,7 @@ import textwrap import unittest +from unittest.mock import patch from test import support, mock_socket try: @@ -549,7 +550,10 @@ def testSendMessageMultipleResentRaises(self): smtp.send_message(m) smtp.close() - def testSendMessageAddDateIfMissing(self): + @patch('email.utils.formatdate') + def testSendMessageAddDateIfMissing(self, mocked_date_obj): + current_date = 'Thu, 1 Jan 1970 17:42:00 +0000' + mocked_date_obj.return_value = current_date m = email.mime.text.MIMEText('A test message') m['From'] = 'foo@bar.com' m['To'] = 'John' @@ -572,8 +576,7 @@ def testSendMessageAddDateIfMissing(self): mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) self.assertEqual(self.output.getvalue(), mexpect) debugout = smtpd.DEBUGSTREAM.getvalue() - current_moment = email.utils.formatdate() - Date = re.compile(''.join(("\\\\nDate: ",current_moment[:16])),re.MULTILINE) + Date = re.compile(''.join(("\\\\nDate: ", current_date)), re.MULTILINE) self.assertRegex(debugout, Date) From aaefe854ea6e8b1798519a52ca341f748113423b Mon Sep 17 00:00:00 2001 From: lafe3402 Date: Thu, 20 Jul 2017 13:51:35 -0400 Subject: [PATCH 4/6] bpo-28879 : Fix test case with less '\' --- Lib/test/test_smtplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index 62a43536841617..c4718673ac0b1d 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -576,7 +576,7 @@ def testSendMessageAddDateIfMissing(self, mocked_date_obj): mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) self.assertEqual(self.output.getvalue(), mexpect) debugout = smtpd.DEBUGSTREAM.getvalue() - Date = re.compile(''.join(("\\\\nDate: ", current_date)), re.MULTILINE) + Date = re.compile(''.join(("\\nDate: ", current_date)), re.MULTILINE) self.assertRegex(debugout, Date) From 9c1a12176314524d15ad2190e4736e15a5a820a4 Mon Sep 17 00:00:00 2001 From: lafe3402 Date: Thu, 3 Aug 2017 10:36:11 -0400 Subject: [PATCH 5/6] bpo-28879 : escape the date example string --- Lib/test/test_smtplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index c4718673ac0b1d..93bb5aef16bba2 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -576,7 +576,7 @@ def testSendMessageAddDateIfMissing(self, mocked_date_obj): mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) self.assertEqual(self.output.getvalue(), mexpect) debugout = smtpd.DEBUGSTREAM.getvalue() - Date = re.compile(''.join(("\\nDate: ", current_date)), re.MULTILINE) + Date = re.compile(''.join(("\\\\nDate: ", re.escape(current_date))), re.MULTILINE) self.assertRegex(debugout, Date) From 178bc12d72457a95a9bb8fb7fe7ee17f3ccab6c7 Mon Sep 17 00:00:00 2001 From: lafe3402 Date: Sat, 13 Jan 2018 15:59:17 -0500 Subject: [PATCH 6/6] Adding news file to advertise changes done by this PR. --- .../next/Library/2018-01-13-15-52-00.bpo-28879.aW2gj0.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-01-13-15-52-00.bpo-28879.aW2gj0.rst diff --git a/Misc/NEWS.d/next/Library/2018-01-13-15-52-00.bpo-28879.aW2gj0.rst b/Misc/NEWS.d/next/Library/2018-01-13-15-52-00.bpo-28879.aW2gj0.rst new file mode 100644 index 00000000000000..448d3bcd363d44 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-01-13-15-52-00.bpo-28879.aW2gj0.rst @@ -0,0 +1,2 @@ +Fix ``smtplib.send_message`` to add Date header if it is missing as per +RFC5322. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy