Fix: Makes the email date aware as soon as possible during parsing (#13266)

This commit is contained in:
Trenton H
2026-07-24 15:39:25 +00:00
committed by GitHub
parent 3fe6562c57
commit b66182cd7b
2 changed files with 39 additions and 4 deletions
+4 -4
View File
@@ -272,10 +272,7 @@ class MailDocumentParser:
logger.debug("Building formatted text from email")
self._text = build_formatted_text(mail)
if is_naive(mail.date):
self._date = make_aware(mail.date)
else:
self._date = mail.date
self._date = mail.date
logger.debug("Creating a PDF from the email")
if self._mailrule_id:
@@ -502,6 +499,9 @@ class MailDocumentParser:
f"Could not parse {filepath}: {err}",
) from err
if is_naive(parsed.date):
parsed.date = make_aware(parsed.date)
return parsed
def tika_parse(self, html: str) -> str:
@@ -145,6 +145,41 @@ class TestEmailFileParsing:
assert parsed_msg.text == "This is just a simple Text Mail.\n"
assert parsed_msg.to == ("some@one.de",)
def test_parse_file_to_message_makes_naive_date_aware(
self,
mocker: MockerFixture,
mail_parser: MailDocumentParser,
simple_txt_email_file: Path,
) -> None:
"""
GIVEN:
- An email whose parsed date is naive (no tzinfo)
WHEN:
- The .eml file is parsed into a MailMessage
THEN:
- The resulting message date is made timezone-aware
"""
mock_message = mocker.Mock(
from_values="mail@someserver.de",
date=datetime.datetime(2022, 10, 12, 21, 40, 43),
)
mocker.patch(
"paperless.parsers.mail.MailMessage.from_bytes",
return_value=mock_message,
)
parsed_msg = mail_parser.parse_file_to_message(simple_txt_email_file)
assert timezone.is_aware(parsed_msg.date)
assert parsed_msg.date.replace(tzinfo=None) == datetime.datetime(
2022,
10,
12,
21,
40,
43,
)
class TestEmailMetadataExtraction:
"""