also handle other text encodings than UTF8

This commit is contained in:
j3d1 2024-04-22 20:05:53 +02:00
parent b3c2233454
commit 49d3b02b9c
2 changed files with 63 additions and 5 deletions

View file

@ -122,7 +122,7 @@ def parse_email_body(raw, log=None):
# skip any text/plain (txt) attachments
if ctype == 'text/plain' and 'attachment' not in cdispo:
segment = part.get_payload(decode=True).decode('utf-8')
segment = part.get_payload()
if not segment:
continue
segment = unescape_and_decode_quoted_printable(segment)
@ -146,11 +146,11 @@ def parse_email_body(raw, log=None):
log.info("Attachment", ctype, cdispo)
else:
if parsed.get_content_type() == 'text/plain':
body = parsed.get_payload(decode=True).decode('utf-8')
body = parsed.get_payload()
elif parsed.get_content_type() == 'text/html':
from bs4 import BeautifulSoup
import re
body = parsed.get_payload(decode=True).decode('utf-8')
body = parsed.get_payload()
soup = BeautifulSoup(body, 'html.parser')
body = re.sub(r'([\r\n]+.?)*[\r\n]', r'\n', soup.get_text()).strip('\n')
else:
@ -192,7 +192,7 @@ def receive_email(envelope, log=None):
email = Email.objects.create(
sender=sender, recipient=recipient, body=body, subject=subject, reference=header_message_id,
in_reply_to=header_in_reply_to, raw=envelope.content.decode('utf-8'), event=target_event,
in_reply_to=header_in_reply_to, raw=envelope.content, event=target_event,
issue_thread=active_issue_thread)
for attachment in attachments:
email.attachments.add(attachment)
@ -243,7 +243,9 @@ class LMTPHandler:
log.info('Message for %s' % envelope.rcpt_tos)
log.info('Message data:\n')
content = None
try:
content = envelope.content
email, new, reply = await sync_to_async(receive_email)(envelope, log)
log.info(f"Created email {email.id}")
systemevent = await sync_to_async(SystemEvent.objects.create)(type='email received', reference=email.id)
@ -260,5 +262,9 @@ class LMTPHandler:
return '250 Message accepted for delivery'
except Exception as e:
log.error(type(e), e)
import uuid
random_filename = 'mail-' + str(uuid.uuid4())
with open(random_filename, 'wb') as f:
f.write(content)
log.error(type(e), e, f"Saved email to {random_filename}")
return '451 Internal server error'