diff --git a/app/email/status.py b/app/email/status.py index 7b93dbd7..3de514aa 100644 --- a/app/email/status.py +++ b/app/email/status.py @@ -59,4 +59,5 @@ E522 = ( "at a rate that prevents additional messages from being delivered." ) E523 = "550 SL E523 Unknown error" +E524 = "550 SL E524 Wrong use of reverse-alias" # endregion diff --git a/app/models.py b/app/models.py index da43fcd3..828c9670 100644 --- a/app/models.py +++ b/app/models.py @@ -1574,7 +1574,7 @@ class Contact(Base, ModelMixin): # make sure contact.website_email isn't a reverse alias orig_contact = Contact.get_by(reply_email=website_email) if orig_contact: - raise CannotCreateContactForReverseAlias + raise CannotCreateContactForReverseAlias(str(orig_contact)) Session.add(new_contact) diff --git a/email_handler.py b/email_handler.py index 635c1920..755dbbd0 100644 --- a/email_handler.py +++ b/email_handler.py @@ -133,6 +133,7 @@ from app.errors import ( VERPForward, VERPReply, MailSentFromReverseAlias, + CannotCreateContactForReverseAlias, ) from app.log import LOG, set_message_id from app.models import ( @@ -2335,6 +2336,21 @@ class MailHandler: try: ret = self._handle(envelope, msg) return ret + + # happen if reverse-alias is used during the forward phase + # as in this case, a new reverse-alias needs to be created for this reverse-alias -> chaos + except CannotCreateContactForReverseAlias as e: + LOG.w( + "Probably due to reverse-alias used in the forward phase, " + "error:%s mail_from:%s, rcpt_tos:%s, header_from:%s, header_to:%s", + e, + envelope.mail_from, + envelope.rcpt_tos, + msg[headers.FROM], + msg[headers.TO], + ) + Session.rollback() + return status.E524 except Exception as e: LOG.e( "email handling fail with error:%s mail_from:%s, rcpt_tos:%s, header_from:%s, header_to:%s, saved to %s",