From 3897f6b633bd7dd7777b1a10f0748f5071d3109c Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Wed, 2 Jun 2021 11:38:25 +0200 Subject: [PATCH] refactor handle_bounce() --- app/models.py | 6 ++++++ email_handler.py | 33 +++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/models.py b/app/models.py index feb1aae7..bceab322 100644 --- a/app/models.py +++ b/app/models.py @@ -1510,6 +1510,12 @@ class EmailLog(db.Model, ModelMixin): else: return "forward" + def get_phase(self) -> str: + if self.is_reply: + return "reply" + else: + return "forward" + def __repr__(self): return f"" diff --git a/email_handler.py b/email_handler.py index 432a6d87..717993fa 100644 --- a/email_handler.py +++ b/email_handler.py @@ -1463,19 +1463,25 @@ def handle_transactional_bounce(envelope: Envelope, rcpt_to): Bounce.create(email=transactional.email, commit=True) -def handle_bounce(envelope, rcpt_to, msg: Message) -> str: +def handle_bounce(envelope, email_log: EmailLog, msg: Message) -> str: """ Return SMTP status, e.g. "500 Error" """ - LOG.d("handle bounce sent to %s", rcpt_to) - - # parse the EmailLog - email_log_id = parse_id_from_bounce(rcpt_to) - email_log = EmailLog.get(email_log_id) if not email_log: + LOG.w("No such email log") return "550 SL E27 No such email log" + contact: Contact = email_log.contact + alias = contact.alias + LOG.d( + "handle bounce for %s, phase=%s, contact=%s, alias=%s", + email_log, + email_log.get_phase(), + contact, + alias, + ) + if email_log.is_reply: content_type = msg.get_content_type().lower() @@ -1573,23 +1579,22 @@ def handle(envelope: Envelope) -> str: handle_transactional_bounce(envelope, rcpt_tos[0]) return "250 bounce handled" - # whether this is a bounce report - is_bounce = False - + # Handle bounce if ( len(rcpt_tos) == 1 and rcpt_tos[0].startswith(BOUNCE_PREFIX) and rcpt_tos[0].endswith(BOUNCE_SUFFIX) ): - is_bounce = True + email_log_id = parse_id_from_bounce(rcpt_tos[0]) + email_log = EmailLog.get(email_log_id) + return handle_bounce(envelope, email_log, msg) if len(rcpt_tos) == 1 and rcpt_tos[0].startswith( f"{BOUNCE_PREFIX_FOR_REPLY_PHASE}+" ): - is_bounce = True - - if is_bounce: - return handle_bounce(envelope, rcpt_tos[0], msg) + email_log_id = parse_id_from_bounce(rcpt_tos[0]) + email_log = EmailLog.get(email_log_id) + return handle_bounce(envelope, email_log, msg) # Whether it's necessary to apply greylisting if greylisting_needed(mail_from, rcpt_tos):