From a8e046400feb947ca8fc20eb5d710a3a11e9b562 Mon Sep 17 00:00:00 2001 From: Andreas Fiehn Date: Wed, 14 Jan 2026 20:19:33 +0100 Subject: [PATCH] fix: Allow ported_string to handle email.header.Header objects, and adding a test case. --- src/mailparser/utils.py | 3 +++ tests/test_utils.py | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/mailparser/utils.py b/src/mailparser/utils.py index c73cd2c..4b5b141 100644 --- a/src/mailparser/utils.py +++ b/src/mailparser/utils.py @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index aea76b7..232b549 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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)