diff -r c47a72627f0c Doc/library/smtplib.rst --- a/Doc/library/smtplib.rst Sun Jan 22 14:41:42 2017 +0800 +++ b/Doc/library/smtplib.rst Mon Jan 23 00:18:45 2017 -0500 @@ -496,7 +496,9 @@ 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 -r c47a72627f0c Lib/smtplib.py --- a/Lib/smtplib.py Sun Jan 22 14:41:42 2017 +0800 +++ b/Lib/smtplib.py Mon Jan 23 00:18:45 2017 -0500 @@ -927,6 +927,12 @@ 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 -r c47a72627f0c Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py Sun Jan 22 14:41:42 2017 +0800 +++ b/Lib/test/test_smtplib.py Mon Jan 23 00:18:45 2017 -0500 @@ -549,6 +549,36 @@ 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):