From f08ca682c005ac1cf9a5a2d324ced30590c320f0 Mon Sep 17 00:00:00 2001 From: Son NK Date: Mon, 30 Dec 2019 18:26:07 +0100 Subject: [PATCH] Create alias on-the-fly if the corresponding custom domain allows catch-all --- email_handler.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/email_handler.py b/email_handler.py index aace400e..7ee1fc98 100644 --- a/email_handler.py +++ b/email_handler.py @@ -44,6 +44,7 @@ from app.email_utils import ( get_email_part, send_email, add_dkim_signature, + get_email_domain_part, ) from app.extensions import db from app.log import LOG @@ -108,7 +109,21 @@ class MailHandler: gen_email = GenEmail.get_by(email=alias) if not gen_email: LOG.d("alias %s not exist") - return "510 Email not exist" + + # check if alias is custom-domain alias and if the custom-domain has catch-all enabled + alias_domain = get_email_domain_part(alias) + custom_domain = CustomDomain.get_by(domain=alias_domain) + if custom_domain and custom_domain.catch_all: + LOG.d("create alias %s for domain %s", alias, custom_domain) + + gen_email = GenEmail.create( + email=alias, + user_id=custom_domain.user_id, + custom_domain_id=custom_domain.id, + ) + db.session.commit() + else: + return "510 Email not exist" user_email = gen_email.user.email