From a6c874e914572ffbe5394f43c877f6963dc3d10a Mon Sep 17 00:00:00 2001 From: Son Nguyen Kim Date: Fri, 17 Sep 2021 17:42:44 +0200 Subject: [PATCH] Use validate_email in get_email_local_part and get_email_domain_part --- app/email_utils.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/email_utils.py b/app/email_utils.py index ac219f6b..309d38bf 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -19,7 +19,7 @@ from typing import Tuple, List, Optional, Union import arrow import dkim import spf -from email_validator import validate_email, EmailNotValidError +from email_validator import validate_email, EmailNotValidError, ValidatedEmail from flanker.addresslib import address from flanker.addresslib.address import EmailAddress from jinja2 import Environment, FileSystemLoader @@ -379,8 +379,12 @@ def get_email_local_part(address) -> str: """ Get the local part from email ab@cd.com -> ab + Convert the local part to lowercase """ - return address[: address.find("@")] + r: ValidatedEmail = validate_email( + address, check_deliverability=False, allow_smtputf8=False + ) + return r.local_part.lower() def get_email_domain_part(address): @@ -388,8 +392,10 @@ def get_email_domain_part(address): Get the domain part from email ab@cd.com -> cd.com """ - address = sanitize_email(address) - return address[address.find("@") + 1 :] + r: ValidatedEmail = validate_email( + address, check_deliverability=False, allow_smtputf8=False + ) + return r.domain # headers used to DKIM sign in order of preference