summaryrefslogtreecommitdiffstats
path: root/Lib/email/mime
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2012-03-23 02:17:51 (GMT)
committerR David Murray <rdmurray@bitdance.com>2012-03-23 02:17:51 (GMT)
commit8680bcc5db7c0edc09b09387c145932da34cd889 (patch)
tree5901ccac2b8e03dd7904d2a65eec65570160599c /Lib/email/mime
parent192195a4fcf3aec27a43169df901667b21ea763a (diff)
downloadcpython-8680bcc5db7c0edc09b09387c145932da34cd889.zip
cpython-8680bcc5db7c0edc09b09387c145932da34cd889.tar.gz
cpython-8680bcc5db7c0edc09b09387c145932da34cd889.tar.bz2
#14380: Have MIMEText defaults to utf-8 when passed non-ASCII unicode
Previously it would just accept the unicode, which would wind up as unicode in the transfer-encoded message object, which is just wrong. Patch by Jeff Knupp.
Diffstat (limited to 'Lib/email/mime')
-rw-r--r--Lib/email/mime/text.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/email/mime/text.py b/Lib/email/mime/text.py
index 5747db5..5a5e39b 100644
--- a/Lib/email/mime/text.py
+++ b/Lib/email/mime/text.py
@@ -27,4 +27,14 @@ class MIMEText(MIMENonMultipart):
"""
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.
+ # XXX: This can be removed once #7304 is fixed.
+ if _charset =='us-ascii':
+ try:
+ _text.encode(_charset)
+ except UnicodeEncodeError:
+ _charset = 'utf-8'
+
self.set_payload(_text, _charset)