from email_validator import EmailNotValidError from email_validator import validate_email as validate_email_address def validate_email(email: str) -> str: if not email: return "" try: validation = validate_email_address(email, check_deliverability=False) # Take the normalized form of the email address for all logic beyond # this point (especially before going to a database query where equality # may not take into account Unicode normalization). email = validation.email except EmailNotValidError as e: # Email is not valid. The exception message is human-readable. email = "" print(str(e)) return email def validate_multiple_emails(emails: str) -> str: if not emails: return "" for email in emails.split(sep=","): email = validate_email(email=email.strip(" ")) if not email: emails = "" break return emails