From ed4acebdb18272d7b451ba8c26f16ddc731b2673 Mon Sep 17 00:00:00 2001 From: Son Date: Sat, 8 Jan 2022 00:28:26 +0100 Subject: [PATCH] delete the email log in reply phase if NonReverseAliasInReplyPhase --- app/config.py | 2 ++ app/models.py | 5 ++++- email_handler.py | 12 +++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/config.py b/app/config.py index 41e694ca..f8059af5 100644 --- a/app/config.py +++ b/app/config.py @@ -315,6 +315,8 @@ ALERT_SPAM_EMAIL = "spam" # When an email is sent from a mailbox to an alias - a cycle ALERT_SEND_EMAIL_CYCLE = "cycle" +ALERT_NON_REVERSE_ALIAS_REPLY_PHASE = "non_reverse_alias_reply_phase" + ALERT_SPF = "spf" # when a mailbox is also an alias diff --git a/app/models.py b/app/models.py index 828c9670..a8aaf478 100644 --- a/app/models.py +++ b/app/models.py @@ -133,9 +133,12 @@ class ModelMixin(object): Session.add(self) @classmethod - def delete(cls, obj_id): + def delete(cls, obj_id, commit=False): Session.query(cls).filter(cls.id == obj_id).delete() + if commit: + Session.commit() + @classmethod def first(cls): return Session.query(cls).first() diff --git a/email_handler.py b/email_handler.py index c6afac6d..d7ee9d99 100644 --- a/email_handler.py +++ b/email_handler.py @@ -85,6 +85,7 @@ from app.config import ( ALERT_HOTMAIL_COMPLAINT_TRANSACTIONAL, ALERT_HOTMAIL_COMPLAINT_REPLY_PHASE, OLD_UNSUBSCRIBER, + ALERT_NON_REVERSE_ALIAS_REPLY_PHASE, ) from app.db import Session from app.email import status, headers @@ -1060,8 +1061,7 @@ def handle_reply(envelope, msg: Message, rcpt_to: str) -> (bool, str): "Cannot encrypt message %s -> %s. %s %s", alias, contact, mailbox, user ) # programming error, user shouldn't see a new email log - EmailLog.delete(email_log.id) - Session.commit() + EmailLog.delete(email_log.id, commit=True) # return 421 so the client can retry later return False, status.E402 @@ -1090,7 +1090,13 @@ def handle_reply(envelope, msg: Message, rcpt_to: str) -> (bool, str): replace_header_when_reply(msg, alias, headers.CC) except NonReverseAliasInReplyPhase as e: LOG.w("non reverse-alias in reply %s %s %s", e, contact, alias) - send_email( + + # the email is ignored, delete the email log + EmailLog.delete(email_log.id, commit=True) + + send_email_at_most_times( + user, + ALERT_NON_REVERSE_ALIAS_REPLY_PHASE, mailbox.email, f"Email sent to {contact.email} contains non reverse-alias addresses", render(