delete the email log in reply phase if NonReverseAliasInReplyPhase

This commit is contained in:
Son 2022-01-08 00:28:26 +01:00
parent b23f9fa971
commit ed4acebdb1
3 changed files with 15 additions and 4 deletions

View File

@ -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

View File

@ -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()

View File

@ -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(