diff --git a/email_handler.py b/email_handler.py index ffdcca4d..e932893e 100644 --- a/email_handler.py +++ b/email_handler.py @@ -563,7 +563,7 @@ def apply_dmarc_policy( # DmarcCheckResult.soft_fail, ): LOG.w( - f"put email from {contact} to {alias} to quarantine. {spam_result}, " + f"put email from {contact} to {alias} to quarantine. {spam_result.event_data()}, " f"mail_from:{envelope.mail_from}, from_header: {msg[headers.FROM]}" ) email_log = quarantine_dmarc_failed_email(alias, contact, envelope, msg) @@ -2595,7 +2595,8 @@ class MailHandler: return_status = handle(envelope, msg) elapsed = time.time() - start if return_status[0] == "5": - if get_spamd_result(msg).spf in ( + spamd_result = get_spamd_result(msg) + if spamd_result and get_spamd_result(msg).spf in ( SPFCheckResult.fail, SPFCheckResult.soft_fail, ): diff --git a/tests/example_emls/no_spamd_header.eml b/tests/example_emls/no_spamd_header.eml new file mode 100644 index 00000000..0d552921 --- /dev/null +++ b/tests/example_emls/no_spamd_header.eml @@ -0,0 +1,19 @@ +X-SimpleLogin-Client-IP: 54.39.200.130 +Received-SPF: Softfail (mailfrom) identity=mailfrom; client-ip=34.59.200.130; + helo=relay.somewhere.net; envelope-from=everwaste@gmail.com; + receiver= +Received: from relay.somewhere.net (relay.somewhere.net [34.59.200.130]) + (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) + (No client certificate requested) + by mx1.sldev.ovh (Postfix) with ESMTPS id 6D8C13F069 + for ; Thu, 17 Mar 2022 16:50:20 +0000 (UTC) +Date: Thu, 17 Mar 2022 16:50:18 +0000 +To: {{ alias_email }} +From: somewhere@rainbow.com +Subject: test Thu, 17 Mar 2022 16:50:18 +0000 +Message-Id: <20220317165018.000191@somewhere-5488dd4b6b-7crp6> +X-Mailer: swaks v20201014.0 jetmore.org/john/code/swaks/ +X-Rspamd-Queue-Id: 6D8C13F069 +X-Rspamd-Server: staging1 + +This is a test mailing diff --git a/tests/test_email_handler.py b/tests/test_email_handler.py index 8c2b56ed..1f7cdd59 100644 --- a/tests/test_email_handler.py +++ b/tests/test_email_handler.py @@ -145,3 +145,17 @@ def test_preserve_5xx_with_valid_spf(flask_client): envelope.rcpt_tos = [msg["to"]] result = email_handler.MailHandler()._handle(envelope, msg) assert result == status.E512 + + +def test_preserve_5xx_with_no_header(flask_client): + user = create_random_user() + alias = Alias.create_new_random(user) + msg = load_eml_file( + "no_spamd_header.eml", + {"alias_email": alias.email}, + ) + envelope = Envelope() + envelope.mail_from = BOUNCE_EMAIL.format(999999999999999999) + envelope.rcpt_tos = [msg["to"]] + result = email_handler.MailHandler()._handle(envelope, msg) + assert result == status.E512