summaryrefslogtreecommitdiff
path: root/Lib/email/mime/text.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/email/mime/text.py')
-rw-r--r--Lib/email/mime/text.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/Lib/email/mime/text.py b/Lib/email/mime/text.py
index 5747db5d67..3b5b09f190 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,6 +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.
"""
+
+ # If no _charset was specified, check to 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 is None:
+ try:
+ _text.encode('us-ascii')
+ _charset = 'us-ascii'
+ except UnicodeEncodeError:
+ _charset = 'utf-8'
+
MIMENonMultipart.__init__(self, 'text', _subtype,
**{'charset': _charset})
+
self.set_payload(_text, _charset)