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

This commit is contained in:
Jenkins 2017-01-13 13:14:15 +00:00 committed by Gerrit Code Review
commit 0b1742a547
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)