Skip to content

Commit 8d83e4b

Browse files
matrixisebitdancer
authored andcommitted
bpo-32727: smtplib's SMTP.send_message behaves differently with from_addr and to_addrs (#5451)
Do not pass the name field in the 'from' address in the SMTP envelope.
1 parent 1e17d4a commit 8d83e4b

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/smtplib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ def send_message(self, msg, from_addr=None, to_addrs=None,
933933
from_addr = (msg[header_prefix + 'Sender']
934934
if (header_prefix + 'Sender') in msg
935935
else msg[header_prefix + 'From'])
936+
from_addr = email.utils.getaddresses([from_addr])[0][1]
936937
if to_addrs is None:
937938
addr_fields = [f for f in (msg[header_prefix + 'To'],
938939
msg[header_prefix + 'Bcc'],

Lib/test/test_smtplib.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ class SimSMTPServer(smtpd.SMTPServer):
825825

826826
def __init__(self, *args, **kw):
827827
self._extra_features = []
828+
self._addresses = {}
828829
smtpd.SMTPServer.__init__(self, *args, **kw)
829830

830831
def handle_accepted(self, conn, addr):
@@ -833,7 +834,8 @@ def handle_accepted(self, conn, addr):
833834
decode_data=self._decode_data)
834835

835836
def process_message(self, peer, mailfrom, rcpttos, data):
836-
pass
837+
self._addresses['from'] = mailfrom
838+
self._addresses['tos'] = rcpttos
837839

838840
def add_feature(self, feature):
839841
self._extra_features.append(feature)
@@ -1072,6 +1074,21 @@ def test_send_unicode_without_SMTPUTF8(self):
10721074
self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
10731075
self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
10741076

1077+
def test_name_field_not_included_in_envelop_addresses(self):
1078+
smtp = smtplib.SMTP(
1079+
HOST, self.port, local_hostname='localhost', timeout=3
1080+
)
1081+
self.addCleanup(smtp.close)
1082+
1083+
message = EmailMessage()
1084+
message['From'] = email.utils.formataddr(('Michaël', 'michael@example.com'))
1085+
message['To'] = email.utils.formataddr(('René', 'rene@example.com'))
1086+
1087+
self.assertDictEqual(smtp.send_message(message), {})
1088+
1089+
self.assertEqual(self.serv._addresses['from'], 'michael@example.com')
1090+
self.assertEqual(self.serv._addresses['tos'], ['rene@example.com'])
1091+
10751092

10761093
class SimSMTPUTF8Server(SimSMTPServer):
10771094

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not include name field in SMTP envelope from address. Patch by Stéphane Wirtel

0 commit comments

Comments
 (0)