Skip to content

Commit f6df398

Browse files
fix: Allow ported_string to handle email.header.Header objects, and adding a test case. (#149)
Co-authored-by: Fedele Mantuano <mantuano.fedele@gmail.com>
1 parent 038de32 commit f6df398

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/mailparser/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import base64
2020
import datetime
2121
import email
22+
import email.header
2223
import email.utils
2324
import functools
2425
import hashlib
@@ -107,6 +108,9 @@ def ported_string(raw_data, encoding="utf-8", errors="ignore"):
107108
if not raw_data:
108109
return str()
109110

111+
if isinstance(raw_data, email.header.Header):
112+
return str(raw_data)
113+
110114
if isinstance(raw_data, str):
111115
return raw_data
112116

tests/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,19 @@ def test_receiveds_format_delay_no_previous_date(self):
587587
# But should have a valid date itself
588588
self.assertIsNotNone(result[1].get("date_utc"))
589589

590+
def test_ported_string_handles_header_object(self):
591+
"""
592+
Test that ported_string can accept an email.header.Header object
593+
and return a decoded string without crashing.
594+
"""
595+
from email.header import Header
596+
597+
raw_val = 'attachment;\r\nfilename="Just a text – 2026.pdf'
598+
header_obj = Header(raw_val, charset="utf-8")
599+
result = ported_string(header_obj)
600+
self.assertIsInstance(result, str)
601+
self.assertEqual(result, raw_val)
602+
590603
def test_parse_received_envelope_from_with_angle_brackets(self):
591604
"""Test utils.py:294-296 — envelope-from clause with angle-bracket match"""
592605
# When envelope-from keyword is present AND its value has angle

0 commit comments

Comments
 (0)