File size: 968 Bytes
b3fd53e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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