diff -r ca696ca204e0 Doc/library/smtplib.rst --- a/Doc/library/smtplib.rst Sat Aug 09 16:40:49 2014 -0400 +++ b/Doc/library/smtplib.rst Tue Aug 12 00:51:09 2014 +0200 @@ -161,6 +161,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 @@ -287,6 +294,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. @@ -294,6 +304,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) @@ -345,7 +358,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` @@ -359,6 +372,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=[]) @@ -413,12 +429,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=[]) @@ -461,6 +484,9 @@ Normally these do not need to be called directly, so they are not documented here. For details, consult the module code. +.. versionadded:: 3.5 + The SMTPUTF8 extension (:RFC:`6531`) is now supported. + .. _smtp-example: diff -r ca696ca204e0 Lib/smtplib.py --- a/Lib/smtplib.py Sat Aug 09 16:40:49 2014 -0400 +++ b/Lib/smtplib.py Tue Aug 12 00:51:09 2014 +0200 @@ -70,6 +70,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. @@ -236,6 +243,7 @@ self._host = host self.timeout = timeout self.esmtp_features = {} + self.encoding = 'ascii' self.source_address = source_address if host: @@ -331,7 +339,7 @@ print('send:', repr(s), file=stderr) if hasattr(self, 'sock') and self.sock: if isinstance(s, str): - s = s.encode("ascii") + s = s.encode(self.encoding) try: self.sock.sendall(s) except OSError: @@ -491,15 +499,40 @@ except SMTPServerDisconnected: pass + def _set_encoding_and_lower_options(self, mail_options): + # Assume mail_options was already parsed if it is a string: + if isinstance(mail_options, str): + return mail_options + self.encoding = 'us-ascii' + self.data_encoding = 'us-ascii' + if self.does_esmtp and mail_options: + lower_options = ' ' + ' '.join(mail_options).lower() + if 'smtputf8' in lower_options: + if self.has_extn('smtputf8') and self.has_extn('8bitmime'): + self.encoding = 'utf-8' + if ('body' in lower_options + and 'body=7bit' not in lower_options): + self.data_encoding = 'utf-8' + else: + raise SMTPNotSupportedError( + 'SMTPUTF8 not supported by server') + return lower_options + return '' + def noop(self): """SMTP 'noop' command -- doesn't do anything :>""" return self.docmd("noop") def mail(self, sender, options=[]): - """SMTP 'mail' command -- begins mail xfer session.""" - optionlist = '' - if options and self.does_esmtp: - optionlist = ' ' + ' '.join(options) + """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 = self._set_encoding_and_lower_options(options) self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist)) return self.getreply() @@ -529,7 +562,7 @@ raise SMTPDataError(code, repl) else: if isinstance(msg, str): - msg = _fix_eols(msg).encode('ascii') + msg = _fix_eols(msg).encode(self.data_encoding) q = _quote_periods(msg) if q[-2:] != bCRLF: q = q + bCRLF @@ -637,13 +670,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() @@ -695,7 +731,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: @@ -761,6 +798,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. @@ -785,16 +825,11 @@ """ self.ehlo_or_helo_if_needed() - esmtp_opts = [] + esmtp_opts = self._set_encoding_and_lower_options(mail_options) 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: - esmtp_opts.append(option) + msg = _fix_eols(msg).encode(self.encoding) + if self.does_esmtp and self.has_extn('size'): + esmtp_opts = "size=%d %s" % (len(msg), esmtp_opts) (code, resp) = self.mail(from_addr, esmtp_opts) if code != 250: if code == 421: diff -r ca696ca204e0 Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py Sat Aug 09 16:40:49 2014 -0400 +++ b/Lib/test/test_smtplib.py Tue Aug 12 00:51:09 2014 +0200 @@ -630,11 +630,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 @@ -721,11 +724,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) @@ -744,7 +752,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) @@ -775,12 +784,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) @@ -789,6 +801,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)