diff --git a/app/email_utils.py b/app/email_utils.py index a0ce7a0a..26194158 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -970,7 +970,10 @@ def add_header(msg: Message, text_header, html_header) -> Message: elif content_type in ("multipart/alternative", "multipart/related"): new_parts = [] for part in msg.get_payload(): - new_parts.append(add_header(part, text_header, html_header)) + if isinstance(part, Message): + new_parts.append(add_header(part, text_header, html_header)) + else: + new_parts.append(part) clone_msg = copy(msg) clone_msg.set_payload(new_parts) return clone_msg diff --git a/email_handler.py b/email_handler.py index d3c50783..a66b6f95 100644 --- a/email_handler.py +++ b/email_handler.py @@ -283,7 +283,7 @@ def get_or_create_reply_to_contact( return contact else: LOG.d( - "create contact %s for alias %s via reply-to header", + "create contact %s for alias %s via reply-to header %s", contact_address, alias, reply_to_header, diff --git a/tests/example_emls/multipart_alternative.eml b/tests/example_emls/multipart_alternative.eml new file mode 100644 index 00000000..27fa7d67 --- /dev/null +++ b/tests/example_emls/multipart_alternative.eml @@ -0,0 +1,25 @@ +Content-Type: multipart/alternative; boundary="===============5006593052976639648==" +MIME-Version: 1.0 +Subject: My subject +From: foo@example.org +To: bar@example.net + +--===============5006593052976639648== +Content-Type: text/plain; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit + +This is HTML +--===============5006593052976639648== +Content-Type: text/html; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: 7bit + + + + This is HTML + + + +--===============5006593052976639648==-- + diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index e5d16744..30c95ae6 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -823,3 +823,15 @@ def test_dmarc_result_na(): def test_dmarc_result_bad_policy(): msg = load_eml_file("dmarc_bad_policy.eml") assert DmarcCheckResult.bad_policy == get_spamd_result(msg).dmarc + + +def test_add_header_multipart_with_invalid_part(): + msg = load_eml_file("multipart_alternative.eml") + parts = msg.get_payload() + ["invalid"] + msg.set_payload(parts) + msg = add_header(msg, "INJECT", "INJECT") + for i, part in enumerate(msg.get_payload()): + if i < 2: + assert part.get_payload().index("INJECT") > -1 + else: + assert part == "invalid"