diff --git a/app/dashboard/views/mailbox.py b/app/dashboard/views/mailbox.py index 35b7518d..b5056be3 100644 --- a/app/dashboard/views/mailbox.py +++ b/app/dashboard/views/mailbox.py @@ -8,10 +8,11 @@ from wtforms.fields.html5 import EmailField from app.config import EMAIL_DOMAIN, ALIAS_DOMAINS, MAILBOX_SECRET, URL from app.dashboard.base import dashboard_bp from app.email_utils import ( - send_email, - render, can_be_used_as_personal_email, email_already_used, + mailbox_already_used, + render, + send_email, ) from app.extensions import db from app.log import LOG @@ -80,7 +81,7 @@ def mailbox_route(): if new_mailbox_form.validate(): mailbox_email = new_mailbox_form.email.data.lower() - if email_already_used(mailbox_email): + if mailbox_already_used(mailbox_email, current_user): flash(f"{mailbox_email} already used", "error") elif not can_be_used_as_personal_email(mailbox_email): flash(f"You cannot use {mailbox_email}.", "error") diff --git a/app/email_utils.py b/app/email_utils.py index 21787731..ca9a8e44 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -348,3 +348,18 @@ def email_already_used(email: str) -> bool: return True return False + + +def mailbox_already_used(email: str, user) -> bool: + if Mailbox.get_by(email=email): + return True + + # support the case user wants to re-add their real email as mailbox + # can happen when user changes their root email and wants to add this new email as mailbox + if email == user.email: + return False + + if User.get_by(email=email): + return True + + return False