From 2b4c82fd1bc4dcc8bf640f6941d400cfc20d504f Mon Sep 17 00:00:00 2001
From: Arkadiy Korotaev
Date: Tue, 13 Apr 2021 14:04:08 +0200
Subject: [PATCH] fix(email): Add ability to attach full first email text to
avoid losing forwards, and to save .eml files for any incoming mesages, plus
fix tests and some minor bugs
---
docs/settings.rst | 4 +
helpdesk/.flake8 | 3 +
helpdesk/email.py | 89 +++++++++++++------
helpdesk/settings.py | 9 ++
helpdesk/templates/helpdesk/ticket.html | 2 +-
.../templates/helpdesk/ticket_desc_table.html | 4 +-
.../tests/test_files/forwarded-message.eml | 27 ++++++
helpdesk/tests/test_get_email.py | 20 ++++-
8 files changed, 128 insertions(+), 30 deletions(-)
create mode 100644 helpdesk/.flake8
create mode 100644 helpdesk/tests/test_files/forwarded-message.eml
diff --git a/docs/settings.rst b/docs/settings.rst
index 5a9b5088..405e8f8d 100644
--- a/docs/settings.rst
+++ b/docs/settings.rst
@@ -223,3 +223,7 @@ The following settings were defined in previous versions and are no longer suppo
- **HELPDESK_FOOTER_SHOW_CHANGE_LANGUAGE_LINK** Is never shown. Use your own template if required.
- **HELPDESK_ENABLE_PER_QUEUE_MEMBERSHIP** Discontinued in favor of HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION.
+
+- **HELPDESK_FULL_FIRST_MESSAGE_FROM_EMAIL** Do not ignore fowarded and replied text from the email messages which create a new ticket; useful for cases when customer forwards some email (error from service or something) and wants support to see that
+
+- **HELPDESK_ALWAYS_SAVE_INCOMING_EMAIL_MESSAGE** Any incoming .eml message is saved and available, helps when customer spent some time doing fancy markup which has been corrupted during the email-to-ticket-comment translate process
diff --git a/helpdesk/.flake8 b/helpdesk/.flake8
new file mode 100644
index 00000000..f119cca1
--- /dev/null
+++ b/helpdesk/.flake8
@@ -0,0 +1,3 @@
+[flake8]
+max-line-length = 120
+import-order-style = pep8
diff --git a/helpdesk/email.py b/helpdesk/email.py
index becc90d0..b1647e31 100644
--- a/helpdesk/email.py
+++ b/helpdesk/email.py
@@ -21,6 +21,7 @@ from os.path import isfile, join
from time import ctime
from bs4 import BeautifulSoup
+from django.conf import settings as django_settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
@@ -394,7 +395,7 @@ def create_object_from_email_message(message, ticket_id, payload, files, logger)
title=_('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}),
date=now,
public=True,
- comment=payload['body'],
+ comment=payload.get('full_body', payload['body']) or "",
message_id=message_id
)
@@ -505,6 +506,7 @@ def object_from_message(message, queue, logger):
ticket = None
body = None
+ full_body = None
counter = 0
files = []
@@ -523,7 +525,19 @@ def object_from_message(message, queue, logger):
if part['Content-Transfer-Encoding'] == '8bit' and part.get_content_charset() == 'utf-8':
body = body.decode('unicode_escape')
body = decodeUnknown(part.get_content_charset(), body)
- body = EmailReplyParser.parse_reply(body)
+ # have to use django_settings here so overwritting it works in tests
+ # the default value is False anyway
+ if ticket is None and getattr(django_settings, 'HELPDESK_FULL_FIRST_MESSAGE_FROM_EMAIL', False):
+ # first message in thread, we save full body to avoid losing forwards and things like that
+ body_parts = []
+ for f in EmailReplyParser.read(body).fragments:
+ body_parts.append(f.content)
+ full_body = '\n\n'.join(body_parts)
+ body = EmailReplyParser.parse_reply(body)
+ else:
+ # second and other reply, save only first part of the message
+ body = EmailReplyParser.parse_reply(body)
+ full_body = body
# workaround to get unicode text out rather than escaped text
try:
body = body.encode('ascii').decode('unicode_escape')
@@ -536,13 +550,23 @@ def object_from_message(message, queue, logger):
except UnicodeDecodeError:
email_body = encoding.smart_text(part.get_payload(decode=False))
- payload = """
-
-
-
-
-%s
-""" % email_body
+ if not body and not full_body:
+ # no text has been parsed so far - try such deep parsing for some messages
+ altered_body = email_body.replace("
", "\n").replace("
{email_body}