mirror of
https://github.com/simple-login/app.git
synced 2025-02-23 15:23:27 +08:00
Create alias on the fly if it belongs to a directory
This commit is contained in:
parent
cdae3c5309
commit
9377e2a0ed
1 changed files with 39 additions and 13 deletions
|
@ -50,7 +50,7 @@ from app.email_utils import (
|
||||||
)
|
)
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.log import LOG
|
from app.log import LOG
|
||||||
from app.models import GenEmail, ForwardEmail, ForwardEmailLog, CustomDomain
|
from app.models import GenEmail, ForwardEmail, ForwardEmailLog, CustomDomain, Directory
|
||||||
from app.utils import random_string
|
from app.utils import random_string
|
||||||
from server import create_app
|
from server import create_app
|
||||||
|
|
||||||
|
@ -112,20 +112,46 @@ class MailHandler:
|
||||||
if not gen_email:
|
if not gen_email:
|
||||||
LOG.d("alias %s not exist")
|
LOG.d("alias %s not exist")
|
||||||
|
|
||||||
# check if alias is custom-domain alias and if the custom-domain has catch-all enabled
|
# try to see if alias could be created on-the-fly
|
||||||
alias_domain = get_email_domain_part(alias)
|
on_the_fly = False
|
||||||
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(
|
# check if alias belongs to a directory. In this case alias has one of the 2 formats:
|
||||||
email=alias,
|
# directory/anything@EMAIL_DOMAIN or directory+anything@EMAIL_DOMAIN
|
||||||
user_id=custom_domain.user_id,
|
if alias.endswith(EMAIL_DOMAIN):
|
||||||
custom_domain_id=custom_domain.id,
|
if "+" in alias or "/" in alias:
|
||||||
automatic_creation=True,
|
if "+" in alias:
|
||||||
)
|
directory_name = alias[alias.find("+")]
|
||||||
db.session.commit()
|
else:
|
||||||
|
directory_name = alias[alias.find("/")]
|
||||||
|
|
||||||
|
directory = Directory.get_by(name=directory_name)
|
||||||
|
if directory:
|
||||||
|
LOG.d("create alias %s for directory %s", alias, directory)
|
||||||
|
on_the_fly = True
|
||||||
|
|
||||||
|
gen_email = GenEmail.create(
|
||||||
|
email=alias,
|
||||||
|
user_id=directory.user_id,
|
||||||
|
directory_id=directory.id,
|
||||||
|
)
|
||||||
|
db.session.commit()
|
||||||
else:
|
else:
|
||||||
|
# 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)
|
||||||
|
on_the_fly = True
|
||||||
|
|
||||||
|
gen_email = GenEmail.create(
|
||||||
|
email=alias,
|
||||||
|
user_id=custom_domain.user_id,
|
||||||
|
custom_domain_id=custom_domain.id,
|
||||||
|
automatic_creation=True,
|
||||||
|
)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
if not on_the_fly:
|
||||||
return "510 Email not exist"
|
return "510 Email not exist"
|
||||||
|
|
||||||
user_email = gen_email.user.email
|
user_email = gen_email.user.email
|
||||||
|
|
Loading…
Reference in a new issue