Merge "Use correct charset when create a text/* type MIME" into stable/newton

This commit is contained in:
Jenkins 2017-01-17 10:00:21 +00:00 committed by Gerrit Code Review
commit eab58cb813
2 changed files with 18 additions and 3 deletions

View File

@ -301,7 +301,15 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
subtype = os.path.splitext(filename)[0]
if content is None:
content = ''
msg = text.MIMEText(content, _subtype=subtype)
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)
return msg

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)