Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/mailparser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import base64
import datetime
import email
import email.header
import email.utils
import functools
import hashlib
Expand Down Expand Up @@ -107,6 +108,9 @@ def ported_string(raw_data, encoding="utf-8", errors="ignore"):
if not raw_data:
return str()

if isinstance(raw_data, email.header.Header):
return str(raw_data)

if isinstance(raw_data, str):
return raw_data

Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ def test_receiveds_format_delay_no_previous_date(self):
# But should have a valid date itself
self.assertIsNotNone(result[1].get("date_utc"))

def test_ported_string_handles_header_object(self):
"""
Test that ported_string can accept an email.header.Header object
and return a decoded string without crashing.
"""
from email.header import Header

raw_val = 'attachment;\r\nfilename="Just a text – 2026.pdf'
header_obj = Header(raw_val, charset="utf-8")
result = ported_string(header_obj)
self.assertIsInstance(result, str)
self.assertEqual(result, raw_val)

def test_parse_received_envelope_from_with_angle_brackets(self):
"""Test utils.py:294-296 — envelope-from clause with angle-bracket match"""
# When envelope-from keyword is present AND its value has angle
Expand Down
Loading