diff --git a/src/mailparser/utils.py b/src/mailparser/utils.py index 7e4f123..dbc6f96 100644 --- a/src/mailparser/utils.py +++ b/src/mailparser/utils.py @@ -19,6 +19,7 @@ import base64 import datetime import email +import email.header import email.utils import functools import hashlib @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index 20f0df0..7665431 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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