Skip to content

Commit

Permalink
Merge dc3bfd1 into 30d37c5
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Oct 12, 2023
2 parents 30d37c5 + dc3bfd1 commit 664e2fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 5 additions & 2 deletions django_yubin/models.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down
25 changes: 24 additions & 1 deletion tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 664e2fe

Please sign in to comment.