diff -r 863f7c57081b Doc/library/smtplib.rst --- a/Doc/library/smtplib.rst Wed May 13 20:32:19 2015 -0400 +++ b/Doc/library/smtplib.rst Wed May 13 22:42:40 2015 -0400 @@ -61,6 +61,10 @@ .. versionchanged:: 3.3 source_address argument was added. + .. versionadded:: 3.5 + The SMTPUTF8 extension (:rfc:`6531`) is now supported. + + .. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, \ certfile=None [, timeout], context=None, \ source_address=None) @@ -161,6 +165,13 @@ The server refused our ``HELO`` message. +.. exception:: SMTPNotSupportedError + + The command or option attempted is not supported by the server. + + .. versionadded:: 3.5 + + .. exception:: SMTPAuthenticationError SMTP authentication went wrong. Most probably the server didn't accept the @@ -291,6 +302,9 @@ :exc:`SMTPAuthenticationError` The server didn't accept the username/password combination. + :exc:`SMTPNotSupportedError` + The ``AUTH`` command is not supported by the server. + :exc:`SMTPException` No suitable authentication method was found. @@ -298,6 +312,9 @@ turn if they are advertised as supported by the server (see :meth:`auth` for a list of supported authentication methods). + .. versionchanged:: 3.5 + :exc:`SMTPNotSupportedError` may be raised. + .. method:: SMTP.auth(mechanism, authobject) @@ -349,7 +366,7 @@ :exc:`SMTPHeloError` The server didn't reply properly to the ``HELO`` greeting. - :exc:`SMTPException` + :exc:`SMTPNotSupportedError` The server does not support the STARTTLS extension. :exc:`RuntimeError` @@ -363,6 +380,9 @@ :attr:`SSLContext.check_hostname` and *Server Name Indicator* (see :data:`~ssl.HAS_SNI`). + .. versionchanged:: 3.5 + :exc:`SMTPNotSupportedError` is used instead of :exc:`SMTPException`. + .. method:: SMTP.sendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]) @@ -417,12 +437,19 @@ The server replied with an unexpected error code (other than a refusal of a recipient). + :exc:`SMTPNotSupportedError` + ``SMTPUTF8`` was given in the *mail_options* but is not supported by the + server. + Unless otherwise noted, the connection will be open even after an exception is raised. .. versionchanged:: 3.2 *msg* may be a byte string. + .. versionchanged:: 3.5 + :exc:`SMTPNotSupportedError` may be raised. + .. method:: SMTP.send_message(msg, from_addr=None, to_addrs=None, \ mail_options=[], rcpt_options=[]) diff -r 863f7c57081b Lib/smtplib.py --- a/Lib/smtplib.py Wed May 13 20:32:19 2015 -0400 +++ b/Lib/smtplib.py Wed May 13 22:42:40 2015 -0400 @@ -71,6 +71,13 @@ class SMTPException(OSError): """Base class for all exceptions raised by this module.""" +class SMTPNotSupportedError(SMTPException): + """The command or option is not supported by the SMTP server. + + This exception is raised when an attempt is made to run a command or a + command with an option which is not supported by the server. + """ + class SMTPServerDisconnected(SMTPException): """Not connected to any SMTP server. @@ -237,6 +244,7 @@ self._host = host self.timeout = timeout self.esmtp_features = {} + self.command_encoding = 'ascii' self.source_address = source_address if host: @@ -337,7 +345,10 @@ self._print_debug('send:', repr(s)) if hasattr(self, 'sock') and self.sock: if isinstance(s, str): - s = s.encode("ascii") + # send is used by the 'data' command, where command_encoding + # should not be used, but 'data' needs to convert the string to + # binary itself anyway, so that's not a problem. + s = s.encode(self.command_encoding) try: self.sock.sendall(s) except OSError: @@ -482,6 +493,7 @@ def rset(self): """SMTP 'rset' command -- resets session.""" + self.command_encoding = 'ascii' return self.docmd("rset") def _rset(self): @@ -501,9 +513,22 @@ return self.docmd("noop") def mail(self, sender, options=[]): - """SMTP 'mail' command -- begins mail xfer session.""" + """SMTP 'mail' command -- begins mail xfer session. + + This method may raise the following exceptions: + + SMTPNotSupportedError The options parameter includes 'SMTPUTF8' + but the SMTPUTF8 extension is not supported by + the server. + """ optionlist = '' if options and self.does_esmtp: + if any(x.lower()=='smtputf8' for x in options): + if self.has_extn('smtputf8'): + self.command_encoding = 'utf-8' + else: + raise SMTPNotSupportedError( + 'SMTPUTF8 not supported by server') optionlist = ' ' + ' '.join(options) self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist)) return self.getreply() @@ -642,13 +667,16 @@ the helo greeting. SMTPAuthenticationError The server didn't accept the username/ password combination. + SMTPNotSupportedError The AUTH command is not supported by the + server. SMTPException No suitable authentication method was found. """ self.ehlo_or_helo_if_needed() if not self.has_extn("auth"): - raise SMTPException("SMTP AUTH extension not supported by server.") + raise SMTPNotSupportedError( + "SMTP AUTH extension not supported by server.") # Authentication methods the server claims to support advertised_authlist = self.esmtp_features["auth"].split() @@ -700,7 +728,8 @@ """ self.ehlo_or_helo_if_needed() if not self.has_extn("starttls"): - raise SMTPException("STARTTLS extension not supported by server.") + raise SMTPNotSupportedError( + "STARTTLS extension not supported by server.") (resp, reply) = self.docmd("STARTTLS") if resp == 220: if not _have_ssl: @@ -765,6 +794,9 @@ SMTPDataError The server replied with an unexpected error code (other than a refusal of a recipient). + SMTPNotSupportedError The mail_options parameter includes 'SMTPUTF8' + but the SMTPUTF8 extension is not supported by + the server. Note: the connection will be open even after an exception is raised. @@ -793,8 +825,6 @@ if isinstance(msg, str): msg = _fix_eols(msg).encode('ascii') if self.does_esmtp: - # Hmmm? what's this? -ddm - # self.esmtp_features['7bit']="" if self.has_extn('size'): esmtp_opts.append("size=%d" % len(msg)) for option in mail_options: diff -r 863f7c57081b Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py Wed May 13 20:32:19 2015 -0400 +++ b/Lib/test/test_smtplib.py Wed May 13 22:42:40 2015 -0400 @@ -651,11 +651,14 @@ super(SimSMTPChannel, self).__init__(*args, **kw) def smtp_EHLO(self, arg): - resp = ('250-testhost\r\n' - '250-EXPN\r\n' - '250-SIZE 20000000\r\n' - '250-STARTTLS\r\n' - '250-DELIVERBY\r\n') + resp = ( + '250-testhost\r\n' + '250-EXPN\r\n' + '250-SIZE 20000000\r\n' + '250-STARTTLS\r\n' + '250-DELIVERBY\r\n' + '250-SMTPUTF8\r\n' + '250-8BITMIME\r\n') resp = resp + self._extrafeatures + '250 HELP' self.push(resp) self.seen_greeting = arg @@ -742,11 +745,16 @@ def handle_accepted(self, conn, addr): self._SMTPchannel = self.channel_class( self._extra_features, self, conn, addr, - decode_data=self._decode_data) + decode_data=self._decode_data, + enable_SMTPUTF8=self.enable_SMTPUTF8, + ) def process_message(self, peer, mailfrom, rcpttos, data): pass + def process_smtputf8_message(self, peer, mailfrom, rcpttos, data): + pass + def add_feature(self, feature): self._extra_features.append(feature) @@ -765,7 +773,8 @@ self.serv_evt = threading.Event() self.client_evt = threading.Event() # Pick a random unused port by passing 0 for the port number - self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=True) + self.serv = SimSMTPServer( + (HOST, 0), ('nowhere', -1), enable_SMTPUTF8=True) # Keep a note of what port was assigned self.port = self.serv.socket.getsockname()[1] serv_args = (self.serv, self.serv_evt, self.client_evt) @@ -796,12 +805,15 @@ self.assertEqual(smtp.esmtp_features, {}) # features expected from the test server - expected_features = {'expn':'', - 'size': '20000000', - 'starttls': '', - 'deliverby': '', - 'help': '', - } + expected_features = { + 'expn':'', + 'size': '20000000', + 'starttls': '', + 'deliverby': '', + 'help': '', + 'smtputf8': '', + '8bitmime': '', + } smtp.ehlo() self.assertEqual(smtp.esmtp_features, expected_features) @@ -810,6 +822,115 @@ self.assertFalse(smtp.has_extn('unsupported-feature')) smtp.quit() + def test_send_unicode_without_SMTPUTF8(self): + m = '¡a test message containing unicode!' + smtp = smtplib.SMTP( + HOST, self.port, local_hostname='localhost', timeout=3) + self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Bob', m) + self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice') + smtp.quit() + + def test_send_unicode_with_SMTPUTF8_via_sendmail(self): + m = '¡a test message containing unicode!' + smtp = smtplib.SMTP( + HOST, self.port, local_hostname='localhost', timeout=3) + smtp.ehlo() + self.assertTrue(smtp.does_esmtp) + self.assertTrue(smtp.has_extn('smtputf8')) + smtp.sendmail('John', 'Sally', m, + mail_options=['BODY=8BITMIME', 'SMTPUTF8']) + smtp.quit() + + def test_send_unicode_with_SMTPUTF8_via_low_level_API(self): + m = '¡a test message containing unicode!' + smtp = smtplib.SMTP( + HOST, self.port, local_hostname='localhost', timeout=3) + smtp.ehlo() + self.assertTrue(smtp.does_esmtp) + self.assertTrue(smtp.has_extn('smtputf8')) + self.assertEqual( + smtp.mail('Jö', options=['BODY=8BITMIME', 'SMTPUTF8']), + (250, b'OK')) + self.assertEqual(smtp.rcpt('János'), (250, b'OK')) + self.assertEqual(smtp.data(m), (250, b'OK')) + smtp.quit() + +## This reveals a bug in smtpd: +# def test_exception_raised_on_unicode_data_with_SMTPUTF8_without_BODY(self): +# m = '¡a test message containing unicode!' +# smtp = smtplib.SMTP( +# HOST, self.port, local_hostname='localhost', timeout=3) +# smtp.ehlo() +# self.assertTrue(smtp.does_esmtp) +# self.assertTrue(smtp.has_extn('smtputf8')) +# self.assertEqual( +# smtp.mail('Peter', options=['SMTPUTF8']), +# (250, b'OK')) +# self.assertEqual(smtp.rcpt('Jessy'), (250, b'OK')) +# self.assertEqual(smtp.data(m), (250, b'OK')) +# self.assertRaises(UnicodeEncodeError, smtp.data, m) +# smtp.quit() + + def test_notsupportederror_on_server_without_smtputf8(self): + m = '¡a test message containing unicode!' + class ChannelClass(SimSMTPChannel): + def smtp_EHLO(self, arg): + resp = ( + '250-testhost\r\n' + '250-EXPN\r\n' + '250-SIZE 20000000\r\n' + '250-STARTTLS\r\n' + '250-DELIVERBY\r\n') + resp = resp + self._extrafeatures + '250 HELP' + self.push(resp) + self.seen_greeting = arg + self.extended_smtp = True + self.serv.channel_class = ChannelClass + smtp = smtplib.SMTP( + HOST, self.port, local_hostname='localhost', timeout=3) + smtp.ehlo() + self.assertTrue(smtp.does_esmtp) + self.assertFalse(smtp.has_extn('smtputf8')) + self.assertRaises( + smtplib.SMTPNotSupportedError, + smtp.sendmail, + 'John', 'Sally', m, mail_options=['BODY=8BITMIME', 'SMTPUTF8']) + self.assertRaises( + smtplib.SMTPNotSupportedError, + smtp.mail, 'John', options=['BODY=8BITMIME', 'SMTPUTF8']) + smtp.close() + + def test_notsupportederror_on_server_lying_about_smtputf8_support(self): + m = '¡a test message containing unicode!' + class ChannelClass(SimSMTPChannel): + def smtp_EHLO(self, arg): + resp = ( + '250-testhost\r\n' + '250-EXPN\r\n' + '250-SIZE 20000000\r\n' + '250-STARTTLS\r\n' + '250-SMTPUTF8\r\n' + '250-DELIVERBY\r\n') + resp = resp + self._extrafeatures + '250 HELP' + self.push(resp) + self.seen_greeting = arg + self.extended_smtp = True + self.serv.channel_class = ChannelClass + smtp = smtplib.SMTP( + HOST, self.port, local_hostname='localhost', timeout=3) + smtp.ehlo() + self.assertTrue(smtp.does_esmtp) + self.assertTrue(smtp.has_extn('smtputf8')) + self.assertFalse(smtp.has_extn('8bitmime')) + self.assertRaises( + smtplib.SMTPNotSupportedError, + smtp.sendmail, + 'John', 'Sally', m, mail_options=['BODY=8BITMIME', 'SMTPUTF8']) + self.assertRaises( + smtplib.SMTPNotSupportedError, + smtp.mail, 'John', options=['BODY=8BITMIME', 'SMTPUTF8']) + smtp.close() + def testVRFY(self): smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)