Skip to content

Commit 3782def

Browse files
authored
gh-65495: Use lowercase mail from and rcpt to in smtplib.SMTP (#107019)
Use lowercase `mail from` and `rcpt to` in `smtplib.SMTP` SMTP commands are case-insensitive. `smtplib` uses lowercase commands, however it writes `mail FROM` and `rcpt TO`, lacking consistency.
1 parent d228825 commit 3782def

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Lib/smtplib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -542,15 +542,15 @@ def mail(self, sender, options=()):
542542
raise SMTPNotSupportedError(
543543
'SMTPUTF8 not supported by server')
544544
optionlist = ' ' + ' '.join(options)
545-
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
545+
self.putcmd("mail", "from:%s%s" % (quoteaddr(sender), optionlist))
546546
return self.getreply()
547547

548548
def rcpt(self, recip, options=()):
549549
"""SMTP 'rcpt' command -- indicates 1 recipient for this mail."""
550550
optionlist = ''
551551
if options and self.does_esmtp:
552552
optionlist = ' ' + ' '.join(options)
553-
self.putcmd("rcpt", "TO:%s%s" % (quoteaddr(recip), optionlist))
553+
self.putcmd("rcpt", "to:%s%s" % (quoteaddr(recip), optionlist))
554554
return self.getreply()
555555

556556
def data(self, msg):

Lib/test/test_smtplib.py

+14
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ class SimSMTPChannel(smtpd.SMTPChannel):
831831
def __init__(self, extra_features, *args, **kw):
832832
self._extrafeatures = ''.join(
833833
[ "250-{0}\r\n".format(x) for x in extra_features ])
834+
self.all_received_lines = []
834835
super(SimSMTPChannel, self).__init__(*args, **kw)
835836

836837
# AUTH related stuff. It would be nice if support for this were in smtpd.
@@ -845,6 +846,7 @@ def found_terminator(self):
845846
self.smtp_state = self.COMMAND
846847
self.push('%s %s' % (e.smtp_code, e.smtp_error))
847848
return
849+
self.all_received_lines.append(self.received_lines)
848850
super().found_terminator()
849851

850852

@@ -1349,6 +1351,18 @@ def test_name_field_not_included_in_envelop_addresses(self):
13491351
self.assertEqual(self.serv._addresses['from'], 'michael@example.com')
13501352
self.assertEqual(self.serv._addresses['tos'], ['rene@example.com'])
13511353

1354+
def test_lowercase_mail_from_rcpt_to(self):
1355+
m = 'A test message'
1356+
smtp = smtplib.SMTP(
1357+
HOST, self.port, local_hostname='localhost',
1358+
timeout=support.LOOPBACK_TIMEOUT)
1359+
self.addCleanup(smtp.close)
1360+
1361+
smtp.sendmail('John', 'Sally', m)
1362+
1363+
self.assertIn(['mail from:<John> size=14'], self.serv._SMTPchannel.all_received_lines)
1364+
self.assertIn(['rcpt to:<Sally>'], self.serv._SMTPchannel.all_received_lines)
1365+
13521366

13531367
class SimSMTPUTF8Server(SimSMTPServer):
13541368

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use lowercase ``mail from`` and ``rcpt to`` in :class:`smptlib.SMTP`.

0 commit comments

Comments
 (0)