diff options
-rw-r--r-- | Lib/email/mime/text.py | 16 | ||||
-rw-r--r-- | Lib/test/test_email/test_email.py | 3 |
2 files changed, 10 insertions, 9 deletions
diff --git a/Lib/email/mime/text.py b/Lib/email/mime/text.py index 5a5e39b..80ff950 100644 --- a/Lib/email/mime/text.py +++ b/Lib/email/mime/text.py @@ -14,7 +14,7 @@ from email.mime.nonmultipart import MIMENonMultipart class MIMEText(MIMENonMultipart): """Class for generating text/* type MIME documents.""" - def __init__(self, _text, _subtype='plain', _charset='us-ascii'): + def __init__(self, _text, _subtype='plain', _charset=None): """Create a text/* type MIME document. _text is the string for this message object. @@ -25,16 +25,18 @@ class MIMEText(MIMENonMultipart): header. This defaults to "us-ascii". Note that as a side-effect, the Content-Transfer-Encoding header will also be set. """ - 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 no _charset was specified, check to see see if there are non-ascii + # characters present. If not, use 'us-ascii', otherwise use utf-8. # XXX: This can be removed once #7304 is fixed. - if _charset =='us-ascii': + if _charset is None: try: - _text.encode(_charset) + _text.encode('us-ascii') + _charset = 'us-ascii' except UnicodeEncodeError: _charset = 'utf-8' + MIMENonMultipart.__init__(self, 'text', _subtype, + **{'charset': _charset}) + self.set_payload(_text, _charset) diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 86aa60c..b07f675 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -621,15 +621,14 @@ class TestMessageAPI(TestEmailBase): # Issue 14291 m = MIMEText('É testabc\n') self.assertEqual(str(m),textwrap.dedent("""\ - MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" + MIME-Version: 1.0 Content-Transfer-Encoding: base64 w4kgdGVzdGFiYwo= """)) - # Test the email.encoders module class TestEncoders(unittest.TestCase): |