diff --git a/app/email_utils.py b/app/email_utils.py index 27c1b48a..bc12eb82 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -161,6 +161,22 @@ def get_email_part(email_from): return email_from +def get_email_local_part(email): + """ + Get the local part from email + ab@cd.com -> ab + """ + return email[: email.find("@")] + + +def get_email_domain_part(email): + """ + Get the domain part from email + ab@cd.com -> cd.com + """ + return email[email.find("@") + 1 :] + + def add_dkim_signature(msg: EmailMessage, email_domain: str): if msg["DKIM-Signature"]: LOG.d("Remove DKIM-Signature %s", msg["DKIM-Signature"]) diff --git a/app/models.py b/app/models.py index 53018ab1..e1d6c026 100644 --- a/app/models.py +++ b/app/models.py @@ -698,3 +698,6 @@ class CustomDomain(db.Model, ModelMixin): def nb_alias(self): return GenEmail.filter_by(custom_domain_id=self.id).count() + + def __repr__(self): + return f"" diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index 47102e04..93106747 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -1,4 +1,9 @@ -from app.email_utils import get_email_name, get_email_part +from app.email_utils import ( + get_email_name, + get_email_part, + get_email_local_part, + get_email_domain_part, +) def test_get_email_name(): @@ -13,3 +18,11 @@ def test_get_email_part(): assert get_email_part("First Last") == "ab@cd.com" assert get_email_part(" First Last ") == "ab@cd.com" assert get_email_part("ab@cd.com") == "ab@cd.com" + + +def test_get_email_local_part(): + assert get_email_local_part("ab@cd.com") == "ab" + + +def test_get_email_domain_part(): + assert get_email_domain_part("ab@cd.com") == "cd.com"