diff -r 13cefcbcc7da Lib/email/mime/text.py --- a/Lib/email/mime/text.py Thu Mar 22 16:24:33 2012 +0100 +++ b/Lib/email/mime/text.py Thu Mar 22 15:36:03 2012 -0400 @@ -27,4 +27,13 @@ """ MIMENonMultipart.__init__(self, 'text', _subtype, **{'charset': _charset}) + + # If _charset was defualted, check to see see if there are non-ascii + # characters present. Default to utf-8 if there are. + if _charset =='us-ascii': + try: + _text.encode(_charset) + except UnicodeEncodeError: + _charset = 'utf-8' + self.set_payload(_text, _charset) diff -r 13cefcbcc7da Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Thu Mar 22 16:24:33 2012 +0100 +++ b/Lib/test/test_email/test_email.py Thu Mar 22 15:36:03 2012 -0400 @@ -617,6 +617,19 @@ abc """)) + def test_unicode_body_defaults_to_utf8_encoding(self): + # Issue 14291 + m = MIMEText('É testabc\n') + self.assertEqual(str(m),textwrap.dedent("""\ + MIME-Version: 1.0 + Content-Type: text/plain; charset="utf-8" + Content-Transfer-Encoding: base64 + + w4kgdGVzdGFiYwo= + """)) + + + # Test the email.encoders module class TestEncoders(unittest.TestCase): @@ -642,7 +655,7 @@ eq(msg['content-transfer-encoding'], '7bit') # Similar, but with 8bit data msg = MIMEText('hello \xf8 world') - eq(msg['content-transfer-encoding'], '8bit') + eq(msg['content-transfer-encoding'], 'base64') # And now with a different charset msg = MIMEText('hello \xf8 world', _charset='iso-8859-1') eq(msg['content-transfer-encoding'], 'quoted-printable')