From 85a92d00bd278022cc00fda6b124c06f614c5ebb Mon Sep 17 00:00:00 2001
From: "Miss Islington (bot)"
 <31488909+miss-islington@users.noreply.github.com>
Date: Wed, 31 Jan 2018 12:54:09 -0800
Subject: bpo-32727: smtplib's SMTP.send_message behaves differently with
 from_addr and to_addrs (GH-5451) (#5455)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Do not pass the name field in the 'from' address in the SMTP envelope.
(cherry picked from commit 8d83e4ba7823827bcbc119db887004d5c3a63dc6)

Co-authored-by: Stéphane Wirtel <stephane@wirtel.be>
---
 Lib/smtplib.py                                        |  1 +
 Lib/test/test_smtplib.py                              | 19 ++++++++++++++++++-
 .../Library/2018-01-30-17-46-18.bpo-32727.aHVsRC.rst  |  1 +
 3 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 Misc/NEWS.d/next/Library/2018-01-30-17-46-18.bpo-32727.aHVsRC.rst

diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index 5e422b7..b679875 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -933,6 +933,7 @@ class SMTP:
             from_addr = (msg[header_prefix + 'Sender']
                            if (header_prefix + 'Sender') in msg
                            else msg[header_prefix + 'From'])
+            from_addr = email.utils.getaddresses([from_addr])[0][1]
         if to_addrs is None:
             addr_fields = [f for f in (msg[header_prefix + 'To'],
                                        msg[header_prefix + 'Bcc'],
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index dd3a6ee..67b3b47 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -818,6 +818,7 @@ class SimSMTPServer(smtpd.SMTPServer):
 
     def __init__(self, *args, **kw):
         self._extra_features = []
+        self._addresses = {}
         smtpd.SMTPServer.__init__(self, *args, **kw)
 
     def handle_accepted(self, conn, addr):
@@ -826,7 +827,8 @@ class SimSMTPServer(smtpd.SMTPServer):
             decode_data=self._decode_data)
 
     def process_message(self, peer, mailfrom, rcpttos, data):
-        pass
+        self._addresses['from'] = mailfrom
+        self._addresses['tos'] = rcpttos
 
     def add_feature(self, feature):
         self._extra_features.append(feature)
@@ -1066,6 +1068,21 @@ class SMTPSimTests(unittest.TestCase):
         self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
         self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
 
+    def test_name_field_not_included_in_envelop_addresses(self):
+        smtp = smtplib.SMTP(
+            HOST, self.port, local_hostname='localhost', timeout=3
+        )
+        self.addCleanup(smtp.close)
+
+        message = EmailMessage()
+        message['From'] = email.utils.formataddr(('Michaël', 'michael@example.com'))
+        message['To'] = email.utils.formataddr(('René', 'rene@example.com'))
+
+        self.assertDictEqual(smtp.send_message(message), {})
+
+        self.assertEqual(self.serv._addresses['from'], 'michael@example.com')
+        self.assertEqual(self.serv._addresses['tos'], ['rene@example.com'])
+
 
 class SimSMTPUTF8Server(SimSMTPServer):
 
diff --git a/Misc/NEWS.d/next/Library/2018-01-30-17-46-18.bpo-32727.aHVsRC.rst b/Misc/NEWS.d/next/Library/2018-01-30-17-46-18.bpo-32727.aHVsRC.rst
new file mode 100644
index 0000000..22c2196
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-01-30-17-46-18.bpo-32727.aHVsRC.rst
@@ -0,0 +1 @@
+Do not include name field in SMTP envelope from address. Patch by Stéphane Wirtel
-- 
cgit v0.12