From b24d58bdf34cb12d5284cc166a8dba35c153df42 Mon Sep 17 00:00:00 2001 From: Son Date: Fri, 31 Dec 2021 12:14:22 +0100 Subject: [PATCH] handle case alias was deleted in the meantime --- email_handler.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/email_handler.py b/email_handler.py index bf61c252..1f99ec70 100644 --- a/email_handler.py +++ b/email_handler.py @@ -51,6 +51,7 @@ from email_validator import validate_email, EmailNotValidError from flanker.addresslib import address from flanker.addresslib.address import EmailAddress from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm.exc import ObjectDeletedError from app import pgp_utils, s3 from app.alias_utils import try_auto_create @@ -547,7 +548,17 @@ def handle_forward(envelope, msg: Message, rcpt_to: str) -> List[Tuple[bool, str from_header = get_header_unicode(msg[headers.FROM]) LOG.d("Create or get contact for from_header:%s", from_header) - contact = get_or_create_contact(from_header, envelope.mail_from, alias) + try: + contact = get_or_create_contact(from_header, envelope.mail_from, alias) + except ObjectDeletedError: + LOG.d("maybe alias was deleted in the meantime") + alias = Alias.get_by(email=alias_address) + if not alias: + LOG.i("Alias %s was deleted in the meantime", alias_address) + if should_ignore_bounce(envelope.mail_from): + return [(True, status.E207)] + else: + return [(False, status.E515)] reply_to_contact = None if msg[headers.REPLY_TO]: