diff --git a/app/email_utils.py b/app/email_utils.py index 82cbd275..0b98cb73 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -11,6 +11,7 @@ from smtplib import SMTP import arrow import dkim from jinja2 import Environment, FileSystemLoader +from validate_email import validate_email from app.config import ( SUPPORT_EMAIL, @@ -645,3 +646,10 @@ def should_add_dkim_signature(domain: str) -> bool: return True return False + + +def is_valid_email(email_address: str) -> bool: + """Used to check whether an email address is valid""" + return validate_email( + email_address=email_address, check_mx=False, use_blacklist=False + ) diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index 75c250bd..1fbe2a59 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -13,6 +13,7 @@ from app.email_utils import ( copy, get_spam_from_header, get_header_from_bounce, + is_valid_email, ) from app.extensions import db from app.models import User, CustomDomain @@ -286,3 +287,10 @@ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simplelogin.co; assert ( get_header_from_bounce(email.message_from_string(msg_str), "Not-exist") is None ) + + +def test_is_valid_email(): + assert is_valid_email("abcd@gmail.com") + assert not is_valid_email("with space@gmail.com") + assert not is_valid_email("strange char !ç@gmail.com") + assert not is_valid_email("emoji👌@gmail.com")