diff --git a/django_yubin/models.py b/django_yubin/models.py index e9e75be..7a1e9db 100644 --- a/django_yubin/models.py +++ b/django_yubin/models.py @@ -1,5 +1,7 @@ import datetime import logging +import email +from email import policy from email import encoders as Encoders from email.mime.base import MIMEBase @@ -15,7 +17,7 @@ from django.utils.text import Truncator from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ -import mailparser +from mailparser import MailParser from . import mailparser_utils, tasks @@ -131,7 +133,8 @@ def recipients(self): return self.to() + self.cc() + self.bcc() def get_message_parser(self): - return mailparser.parse_from_string(self.message_data) + message = email.message_from_string(self.message_data, policy=policy.default) + return MailParser(message) def get_email_message(self): """ diff --git a/tests/tests/test_models.py b/tests/tests/test_models.py index af31aa2..543b80a 100644 --- a/tests/tests/test_models.py +++ b/tests/tests/test_models.py @@ -3,7 +3,7 @@ from email.generator import _fmt from unittest.mock import patch -from django.core.mail import EmailMultiAlternatives +from django.core.mail import EmailMessage, EmailMultiAlternatives from django.test import TestCase from django.utils import timezone @@ -200,3 +200,26 @@ def reset_mock(): with self.subTest("compare message as_string"): self.assertEqual(reconstructed_msg.as_string(), ref_as_string) reset_mock() + + def test_email_with_long_subject(self): + email_message = EmailMessage( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " + "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " + "laboris nisi ut aliquip ex ea commodo consequat.", + "Message body", + 'mail_from@abc.com', + ['mail_to@abc.com'] + ) + + message = Message.objects.create( + to_address=','.join(email_message.to), + cc_address=','.join(email_message.cc), + bcc_address=','.join(email_message.bcc), + from_address=email_message.from_email, + subject=email_message.subject, + message_data=email_message.message().as_string(), + ) + + parsed_message = message.get_email_message() + + self.assertNotIn("\n", parsed_message.subject)