If we cant decode using the body charset, try to decode using utf8. (#4)

If we can't decode using the body charset, try to decode using utf8.
Change the way we get the cahrset.

Task #159072
This commit is contained in:
Juanjo Garcia Pagan 2023-05-16 13:35:49 +02:00 committed by GitHub
parent facc433a8b
commit 916a3d9d96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 4 deletions

View File

@ -3,10 +3,12 @@ import bleach
def render_email(eml):
body = eml.get_body(['html', 'plain'])
if body:
charset = body.get_content_charset()
if not charset:
charset = 'utf-8'
html = body.get_payload(decode=True).decode(charset)
charset = body.get_content_charset('utf-8')
try:
html = body.get_payload(decode=True).decode(charset)
except:
# If the charset returns an error, try utf-8
html = body.get_payload(decode=True).decode('utf-8')
else:
html = ''
images = {}