Use correct charset when create a text/* type MIME

Change-Id: If0a0b8f906c9f4475156600e792021d7d9adc8f3
Closes-Bug: #1599294
This commit is contained in:
huangtianhua 2016-07-07 10:23:42 +08:00
parent 1f7925d8e8
commit 587d12897f
2 changed files with 16 additions and 4 deletions

View File

@ -325,8 +325,13 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
subtype = os.path.splitext(filename)[0]
if content is None:
content = ''
msg = (text.MIMEText(content, _subtype=subtype)
if subtype else text.MIMEText(content))
try:
content.encode('us-ascii')
charset = 'us-ascii'
except UnicodeEncodeError:
charset = 'utf-8'
msg = (text.MIMEText(content, _subtype=subtype, _charset=charset)
if subtype else text.MIMEText(content, _charset=charset))
msg.add_header('Content-Disposition', 'attachment',
filename=filename)

View File

@ -154,8 +154,15 @@ class MultipartMime(software_config.SoftwareConfig):
@staticmethod
def _create_message(part, subtype, filename):
msg = (text.MIMEText(part, _subtype=subtype)
if subtype else text.MIMEText(part))
charset = 'us-ascii'
try:
part.encode(charset)
except UnicodeEncodeError:
charset = 'utf-8'
msg = (text.MIMEText(part, _subtype=subtype,
_charset=charset)
if subtype else text.MIMEText(part, _charset=charset))
if filename:
msg.add_header('Content-Disposition', 'attachment',
filename=filename)