@@ -806,13 +806,15 @@ def testLineTooLong(self):
806806 'Ms.B@xn--fo-fka.com' :'Sally B' ,
807807 'Mrs.C@somewhereesle.com' :'Ruth C' ,
808808 }
809+ sim_utf8_users = {'Ms.É@somewhere.com' : 'Éva É' }
809810
810811sim_auth = ('Mr.A@somewhere.com' , 'somepassword' )
811812sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
812813 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=' )
813814sim_lists = {'list-1' :['Mr.A@somewhere.com' ,'Mrs.C@somewhereesle.com' ],
814815 'list-2' :['Ms.B@xn--fo-fka.com' ,],
815816 }
817+ sim_utf8_lists = {'list-é' : ['Mr.A@somewhere.com' , 'Ms.É@somewhere.com' ]}
816818
817819# Simulated SMTP channel & server
818820class ResponseException (Exception ): pass
@@ -949,23 +951,39 @@ def smtp_EHLO(self, arg):
949951 self .seen_greeting = arg
950952 self .extended_smtp = True
951953
954+ def _strip_smtputf8 (self , arg ):
955+ # RFC 6531 section 3.7.4.2: VRFY and EXPN take an optional SMTPUTF8
956+ # parameter which allows UTF-8 in the reply.
957+ if not arg :
958+ return arg
959+ arg , _ , params = arg .partition (' ' )
960+ params = self ._getparams (params .upper ().split ())
961+ if params and params .pop ('SMTPUTF8' , False ) is True :
962+ self .require_SMTPUTF8 = True
963+ return arg
964+
952965 def smtp_VRFY (self , arg ):
953966 # For max compatibility smtplib should be sending the raw address.
954- if arg in sim_users :
955- self .push ('250 %s %s' % (sim_users [arg ], smtplib .quoteaddr (arg )))
967+ arg = self ._strip_smtputf8 (arg )
968+ users = sim_users | sim_utf8_users
969+ if arg in users :
970+ self .push ('250 %s %s' % (users [arg ], smtplib .quoteaddr (arg )))
956971 else :
957972 self .push ('550 No such user: %s' % arg )
958973
959974 def smtp_EXPN (self , arg ):
975+ arg = self ._strip_smtputf8 (arg )
960976 list_name = arg .lower ()
961- if list_name in sim_lists :
962- user_list = sim_lists [list_name ]
977+ lists = sim_lists | sim_utf8_lists
978+ users = sim_users | sim_utf8_users
979+ if list_name in lists :
980+ user_list = lists [list_name ]
963981 for n , user_email in enumerate (user_list ):
964982 quoted_addr = smtplib .quoteaddr (user_email )
965983 if n < len (user_list ) - 1 :
966- self .push ('250-%s %s' % (sim_users [user_email ], quoted_addr ))
984+ self .push ('250-%s %s' % (users [user_email ], quoted_addr ))
967985 else :
968- self .push ('250 %s %s' % (sim_users [user_email ], quoted_addr ))
986+ self .push ('250 %s %s' % (users [user_email ], quoted_addr ))
969987 else :
970988 self .push ('550 No access for you!' )
971989
@@ -1357,6 +1375,40 @@ def test_send_unicode_without_SMTPUTF8(self):
13571375 self .addCleanup (smtp .close )
13581376 self .assertRaises (UnicodeEncodeError , smtp .sendmail , 'Alice' , 'Böb' , '' )
13591377 self .assertRaises (UnicodeEncodeError , smtp .mail , 'Älice' )
1378+ self .assertRaises (UnicodeEncodeError , smtp .verify , 'Ms.É@somewhere.com' )
1379+ self .assertRaises (UnicodeEncodeError , smtp .expn , 'list-é' )
1380+
1381+ def test_vrfy_NotSupportedError_if_no_SMTPUTF8 (self ):
1382+ smtp = smtplib .SMTP (
1383+ HOST , self .port , local_hostname = 'localhost' ,
1384+ timeout = support .LOOPBACK_TIMEOUT )
1385+ self .addCleanup (smtp .close )
1386+
1387+ smtp .ehlo ()
1388+ self .assertFalse (smtp .has_extn ('smtputf8' ))
1389+ with self .assertRaises (smtplib .SMTPNotSupportedError ):
1390+ smtp .vrfy ('Mr.A@somewhere.com' , smtputf8 = True )
1391+ with self .assertRaises (smtplib .SMTPNotSupportedError ):
1392+ smtp .vrfy ('Ms.É@somewhere.com' , smtputf8 = True )
1393+
1394+ # Nothing was sent, the connection is still usable
1395+ self .assertEqual (smtp .noop (), (250 , b'OK' ))
1396+
1397+ def test_expn_NotSupportedError_if_no_SMTPUTF8 (self ):
1398+ smtp = smtplib .SMTP (
1399+ HOST , self .port , local_hostname = 'localhost' ,
1400+ timeout = support .LOOPBACK_TIMEOUT )
1401+ self .addCleanup (smtp .close )
1402+
1403+ smtp .ehlo ()
1404+ self .assertFalse (smtp .has_extn ('smtputf8' ))
1405+ with self .assertRaises (smtplib .SMTPNotSupportedError ):
1406+ smtp .expn ('list-1' , smtputf8 = True )
1407+ with self .assertRaises (smtplib .SMTPNotSupportedError ):
1408+ smtp .expn ('list-é' , smtputf8 = True )
1409+
1410+ # Nothing was sent, the connection is still usable
1411+ self .assertEqual (smtp .noop (), (250 , b'OK' ))
13601412
13611413 def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8 (self ):
13621414 # This test is located here and not in the SMTPUTF8SimTests
@@ -1485,6 +1537,76 @@ def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
14851537 self .assertIn ('SMTPUTF8' , self .serv .last_mail_options )
14861538 self .assertEqual (self .serv .last_rcpt_options , [])
14871539
1540+ def test_vrfy_with_SMTPUTF8 (self ):
1541+ smtp = smtplib .SMTP (
1542+ HOST , self .port , local_hostname = 'localhost' ,
1543+ timeout = support .LOOPBACK_TIMEOUT )
1544+ self .addCleanup (smtp .close )
1545+
1546+ smtp .ehlo ()
1547+ self .assertTrue (smtp .has_extn ('smtputf8' ))
1548+ self .assertEqual (
1549+ smtp .vrfy ('Ms.É@somewhere.com' , smtputf8 = True ),
1550+ (250 , 'Éva É <Ms.É@somewhere.com>' .encode ('utf-8' ))
1551+ )
1552+ # ASCII reply
1553+ self .assertEqual (
1554+ smtp .vrfy ('Mr.A@somewhere.com' , smtputf8 = True ),
1555+ (250 , b'John A <Mr.A@somewhere.com>' )
1556+ )
1557+ # encoding doesn't leak out of a command
1558+ self .assertEqual (smtp .command_encoding , 'ascii' )
1559+ self .assertEqual (
1560+ smtp .vrfy ('unknown@unknown.com' , smtputf8 = True ),
1561+ (550 , b'No such user: unknown@unknown.com' )
1562+ )
1563+ # Reply stream still in sync
1564+ self .assertEqual (smtp .noop (), (250 , b'OK' ))
1565+
1566+ def test_vrfy_smtputf8_ehlo_if_needed (self ):
1567+ smtp = smtplib .SMTP (
1568+ HOST , self .port , local_hostname = 'localhost' ,
1569+ timeout = support .LOOPBACK_TIMEOUT )
1570+ self .addCleanup (smtp .close )
1571+
1572+ self .assertEqual (
1573+ smtp .vrfy ('Ms.É@somewhere.com' , smtputf8 = True ),
1574+ (250 , 'Éva É <Ms.É@somewhere.com>' .encode ('utf-8' ))
1575+ )
1576+ self .assertTrue (smtp .has_extn ('smtputf8' ))
1577+ # No reply was left unread
1578+ self .assertEqual (smtp .noop (), (250 , b'OK' ))
1579+
1580+ def test_vrfy_smtputf8_helo_fails (self ):
1581+ smtp = smtplib .SMTP (
1582+ HOST , self .port , local_hostname = 'localhost' ,
1583+ timeout = support .LOOPBACK_TIMEOUT )
1584+ self .addCleanup (smtp .close )
1585+
1586+ smtp .helo ()
1587+ with self .assertRaises (smtplib .SMTPNotSupportedError ):
1588+ smtp .vrfy ('Mr.A@somewhere.com' , smtputf8 = True )
1589+ self .assertFalse (smtp .has_extn ('smtputf8' ))
1590+ # Nothing was sent, the connection is still usable
1591+ self .assertEqual (smtp .noop (), (250 , b'OK' ))
1592+
1593+ def test_expn_with_SMTPUTF8 (self ):
1594+ smtp = smtplib .SMTP (
1595+ HOST , self .port , local_hostname = 'localhost' ,
1596+ timeout = support .LOOPBACK_TIMEOUT )
1597+ self .addCleanup (smtp .close )
1598+
1599+ smtp .ehlo ()
1600+ self .assertTrue (smtp .has_extn ('smtputf8' ))
1601+ self .assertEqual (
1602+ smtp .expn ('list-é' , smtputf8 = True ),
1603+ (250 , ('John A <Mr.A@somewhere.com>\n '
1604+ 'Éva É <Ms.É@somewhere.com>' ).encode ('utf-8' )))
1605+ # Encoding doesn't leak out of a command
1606+ self .assertEqual (smtp .command_encoding , 'ascii' )
1607+ # No reply was left unread
1608+ self .assertEqual (smtp .noop (), (250 , b'OK' ))
1609+
14881610 def test_send_unicode_with_SMTPUTF8_via_low_level_API (self ):
14891611 m = '¡a test message containing unicode!' .encode ('utf-8' )
14901612 smtp = smtplib .SMTP (
0 commit comments