diff --git a/core/mail/protocol.py b/core/mail/protocol.py index 00872f0..3639989 100644 --- a/core/mail/protocol.py +++ b/core/mail/protocol.py @@ -53,6 +53,12 @@ def unescape_simplified_quoted_printable(s, encoding='utf-8'): return quopri.decodestring(s).decode(encoding) +def decode_inline_encodings(s): + s = unescape_and_decode_quoted_printable(s) + s = unescape_and_decode_base64(s) + return s + + def ascii_strip(s): if not s: return None @@ -134,8 +140,7 @@ def decode_email_segment(segment, charset, transfer_encoding): import base64 segment = base64.b64decode(segment).decode('utf-8') else: - segment = unescape_and_decode_quoted_printable(segment) - segment = unescape_and_decode_base64(segment) + segment = decode_inline_encodings(segment.decode('utf-8')) return segment @@ -160,7 +165,7 @@ def parse_email_body(raw, log=None): segment = part.get_payload() if not segment: continue - segment = decode_email_segment(segment, charset, part.get('Content-Transfer-Encoding')) + segment = decode_email_segment(segment.encode('utf-8'), charset, part.get('Content-Transfer-Encoding')) log.debug(segment) body = body + segment elif 'attachment' in cdispo or 'inline' in cdispo: @@ -193,7 +198,8 @@ def parse_email_body(raw, log=None): else: log.warning("Unknown content type %s", parsed.get_content_type()) body = "Unknown content type" - body = decode_email_segment(body, parsed.get_content_charset(), parsed.get('Content-Transfer-Encoding')) + body = decode_email_segment(body.encode('utf-8'), parsed.get_content_charset(), + parsed.get('Content-Transfer-Encoding')) log.debug(body) return parsed, body, attachments @@ -221,8 +227,9 @@ def receive_email(envelope, log=None): subject = ascii_strip(parsed.get('Subject')) if not subject: subject = "No subject" - subject = unescape_and_decode_quoted_printable(subject) - subject = unescape_and_decode_base64(subject) + subject = decode_inline_encodings(subject) + recipient = decode_inline_encodings(recipient) + sender = decode_inline_encodings(sender) target_event = find_target_event(recipient) active_issue_thread, new = find_active_issue_thread(header_in_reply_to, recipient, subject, target_event) @@ -256,7 +263,7 @@ do not create a new request. Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid()) reply_email = Email.objects.create( - sender=recipient, recipient=sender, body=body, subject=ascii_strip(subject), + sender=recipient, recipient=sender, body=body, subject=subject, in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread) reply = make_reply(reply_email, references, event=target_event.slug if target_event else None) else: