greco: "detect" plain text attachment encoding (#47236)

This commit is contained in:
Frédéric Péters 2020-10-05 16:23:57 +02:00
parent 17e733f2cc
commit bb943bfbde
1 changed files with 7 additions and 1 deletions

View File

@ -134,7 +134,13 @@ class Greco(BaseResource):
content_type = attachment.get('content_type') or 'application/octet-stream'
maintype, subtype = content_type.split('/', 1)
if maintype == 'text':
part = MIMEText(content, _subtype=subtype)
for encoding in ('utf-8', 'iso-8859-15'):
try:
content.decode(encoding)
break
except UnicodeDecodeError:
pass
part = MIMEText(content, _subtype=subtype, _charset=encoding)
else:
part = MIMEBase(maintype, subtype, name=filename)
attachment['real_bytes'] = content