Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/mailparser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,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
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,15 @@ def test_receiveds_format_delay_no_previous_date(self):
self.assertEqual(result[1]["delay"], 0)
# 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)
Loading