email_validator/__init__.py
def validate_email(
    email,
    allow_smtputf8=True,
    allow_empty_local=False,
    check_deliverability=True,
    test_environment=False,
    timeout=DEFAULT_TIMEOUT,
    dns_resolver=None
):
    """
    Validates an email address, returning an ValidationResult object. The ValidationResult object's property of valid
    will be set to False if the address is not valid or True when the address is valid. The ValidationResult object's
    property of error will be an empty string if the email in valid or it will contain a string-formatted error message
    if invalid. The email argument can be a str or a bytes instance, but if bytes it must be ASCII-only.
    """

    error = ""

    # Allow email to be a str or bytes instance. If bytes,
    # it must be ASCII because that's how the bytes work
    # on the wire with SMTP.
    if not isinstance(email, (str, unicode_class)):
        email = email.decode("ascii")

    # At-sign.
    parts = email.split('@')
    if len(parts) != 2:
        if error == "":
            error = "The email address is not valid. It must have exactly one @-sign."

    # Collect return values in this instance.
    ret = ValidationResult()
    ret.original_email = email

    # Validate the email address's local part syntax and get a normalized form.
    local_part_info = validate_email_local_part(parts[0],
                                                allow_smtputf8=allow_smtputf8,
                                                allow_empty_local=allow_empty_local)
    ret.local_part = local_part_info["local_part"]
    ret.ascii_local_part = local_part_info["ascii_local_part"]
    ret.smtputf8 = local_part_info["smtputf8"]
    if error == "":
        error = local_part_info["error"]

    # Validate the email address's domain part syntax and get a normalized form.
    domain_part_info = validate_email_domain_part(parts[1], test_environment=test_environment)
    ret.domain = domain_part_info["domain"]
    ret.ascii_domain = domain_part_info["ascii_domain"]
    if error == "":
        error = domain_part_info["error"]

    # Construct the complete normalized form.
    ret.email = ret.local_part + "@" + ret.domain

    # If the email address has an ASCII form, add it.
    if not ret.smtputf8:
        ret.ascii_email = ret.ascii_local_part + "@" + ret.ascii_domain

    # If the email address has an ASCII representation, then we assume it may be
    # transmitted in ASCII (we can't assume SMTPUTF8 will be used on all hops to
    # the destination) and the length limit applies to ASCII characters (which is
    # the same as octets). The number of characters in the internationalized form
    # may be many fewer (because IDNA ASCII is verbose) and could be less than 254
    # Unicode characters, and of course the number of octets over the limit may
    # not be the number of characters over the limit, so if the email address is
    # internationalized, we can't give any simple information about why the address
    # is too long.
    #
    # In addition, check that the UTF-8 encoding (i.e. not IDNA ASCII and not
    # Unicode characters) is at most 254 octets. If the addres is transmitted using
    # SMTPUTF8, then the length limit probably applies to the UTF-8 encoded octets.
    # If the email address has an ASCII form that differs from its internationalized
    # form, I don't think the internationalized form can be longer, and so the ASCII
    # form length check would be sufficient. If there is no ASCII form, then we have
    # to check the UTF-8 encoding. The UTF-8 encoding could be up to about four times
    # longer than the number of characters.
    #
    # See the length checks on the local part and the domain.
    if ret.ascii_email and len(ret.ascii_email) > EMAIL_MAX_LENGTH:
        if ret.ascii_email == ret.email:
            reason = __get_length_reason(ret.ascii_email)
        elif len(ret.email) > EMAIL_MAX_LENGTH:
            # If there are more than 254 characters, then the ASCII
            # form is definitely going to be too long.
            reason = __get_length_reason(ret.email, utf8=True)
        else:
            reason = "(when converted to IDNA ASCII)"
        if error == "":
            error = "The email address is too long {}.".format(reason)
    if len(ret.email.encode("utf8")) > EMAIL_MAX_LENGTH:
        if len(ret.email) > EMAIL_MAX_LENGTH:
            # If there are more than 254 characters, then the UTF-8
            # encoding is definitely going to be too long.
            reason = __get_length_reason(ret.email, utf8=True)
        else:
            reason = "(when encoded in bytes)"
        if error == "":
            error = "The email address is too long {}.".format(reason)

    if check_deliverability and not test_environment:
        # Validate the email address's deliverability using DNS
        # and update the return dict with metadata.
        deliverability_info = validate_email_deliverability(
            ret["domain"], ret["domain_i18n"], timeout, dns_resolver
        )
        if "mx" in deliverability_info:
            ret.mx = deliverability_info["mx"]
            ret.mx_fallback_type = deliverability_info["mx-fallback"]
        if "error" in deliverability_info:
            if error == "":
                error = deliverability_info["error"]

    ret.error = error
    if ret.error == "":
        ret.valid = True
    return ret

    Validates an email address, returning an ValidationResult object. The ValidationResult object's property of valid
    will be set to False if the address is not valid or True when the address is valid. The ValidationResult object's
    property of error will be an empty string if the email in valid or it will contain a string-formatted error message
    if invalid. The email argument can be a str or a bytes instance, but if bytes it must be ASCII-only.
    
Validates an email address, returning an ValidationResult object. The ValidationResult object's property of valid
will be set to False if the address is not valid or True when the address is valid. The ValidationResult object's
property of error will be an empty string if the email in valid or it will contain a string-formatted error message
if invalid. The email argument can be a str or a bytes instance, but if bytes it must be ASCII-only.
Validates an email address returning an ValidationResult object . The ValidationResult object ' s property of valid will be set to False if the address is not valid or True when the address is valid . The ValidationResult object ' s property of error will be an empty string if the email in valid or it will contain a string - formatted error message if invalid . The email argument can be a str or a bytes instance but if bytes it must be ASCII - only .
def validate_email(
    email,
    allow_smtputf8=True,
    allow_empty_local=False,
    check_deliverability=True,
    test_environment=False,
    timeout=DEFAULT_TIMEOUT,
    dns_resolver=None
):
    error = ""
    if not isinstance(email, (str, unicode_class)):
        email = email.decode("ascii")
    parts = email.split('@')
    if len(parts) != 2:
        if error == "":
            error = "The email address is not valid. It must have exactly one @-sign."
    ret = ValidationResult()
    ret.original_email = email
    local_part_info = validate_email_local_part(parts[0],
                                                allow_smtputf8=allow_smtputf8,
                                                allow_empty_local=allow_empty_local)
    ret.local_part = local_part_info["local_part"]
    ret.ascii_local_part = local_part_info["ascii_local_part"]
    ret.smtputf8 = local_part_info["smtputf8"]
    if error == "":
        error = local_part_info["error"]
    domain_part_info = validate_email_domain_part(parts[1], test_environment=test_environment)
    ret.domain = domain_part_info["domain"]
    ret.ascii_domain = domain_part_info["ascii_domain"]
    if error == "":
        error = domain_part_info["error"]
    ret.email = ret.local_part + "@" + ret.domain
    if not ret.smtputf8:
        ret.ascii_email = ret.ascii_local_part + "@" + ret.ascii_domain
     In addition, check that the UTF-8 encoding (i.e. not IDNA ASCII and not
     Unicode characters) is at most 254 octets. If the addres is transmitted using
     SMTPUTF8, then the length limit probably applies to the UTF-8 encoded octets.
     If the email address has an ASCII form that differs from its internationalized
     form, I don't think the internationalized form can be longer, and so the ASCII
     form length check would be sufficient. If there is no ASCII form, then we have
     to check the UTF-8 encoding. The UTF-8 encoding could be up to about four times
     longer than the number of characters.
     See the length checks on the local part and the domain.
    if ret.ascii_email and len(ret.ascii_email) > EMAIL_MAX_LENGTH:
        if ret.ascii_email == ret.email:
            reason = __get_length_reason(ret.ascii_email)
        elif len(ret.email) > EMAIL_MAX_LENGTH:
             If there are more than 254 characters, then the ASCII
             form is definitely going to be too long.
            reason = __get_length_reason(ret.email, utf8=True)
        else:
            reason = "(when converted to IDNA ASCII)"
        if error == "":
            error = "The email address is too long {}.".format(reason)
    if len(ret.email.encode("utf8")) > EMAIL_MAX_LENGTH:
        if len(ret.email) > EMAIL_MAX_LENGTH:
             If there are more than 254 characters, then the UTF-8
             encoding is definitely going to be too long.
            reason = __get_length_reason(ret.email, utf8=True)
        else:
            reason = "(when encoded in bytes)"
        if error == "":
            error = "The email address is too long {}.".format(reason)
    if check_deliverability and not test_environment:
         Validate the email address's deliverability using DNS
         and update the return dict with metadata.
        deliverability_info = validate_email_deliverability(
            ret["domain"], ret["domain_i18n"], timeout, dns_resolver
        )
        if "mx" in deliverability_info:
            ret.mx = deliverability_info["mx"]
            ret.mx_fallback_type = deliverability_info["mx-fallback"]
        if "error" in deliverability_info:
            if error == "":
                error = deliverability_info["error"]
    ret.error = error
    if ret.error == "":
        ret.valid = True
    return ret
def validate_email ( email , allow_smtputf8 = True , allow_empty_local = False , check_deliverability = True , test_environment = False , timeout = DEFAULT_TIMEOUT , dns_resolver = None ) : error = "" if not isinstance ( email , ( str , unicode_class ) ) : email = email . decode ( "ascii" ) parts = email . split ( '@' ) if len ( parts ) != 2 : if error == "" : error = "The email address is not valid. It must have exactly one @-sign." ret = ValidationResult ( ) ret . original_email = email local_part_info = validate_email_local_part ( parts [ 0 ] , allow_smtputf8 = allow_smtputf8 , allow_empty_local = allow_empty_local ) ret . local_part = local_part_info [ "local_part" ] ret . ascii_local_part = local_part_info [ "ascii_local_part" ] ret . smtputf8 = local_part_info [ "smtputf8" ] if error == "" : error = local_part_info [ "error" ] domain_part_info = validate_email_domain_part ( parts [ 1 ] , test_environment = test_environment ) ret . domain = domain_part_info [ "domain" ] ret . ascii_domain = domain_part_info [ "ascii_domain" ] if error == "" : error = domain_part_info [ "error" ] ret . email = ret . local_part + "@" + ret . domain if not ret . smtputf8 : ret . ascii_email = ret . ascii_local_part + "@" + ret . ascii_domain if ret . ascii_email and len ( ret . ascii_email ) > EMAIL_MAX_LENGTH : if ret . ascii_email == ret . email : reason = __get_length_reason ( ret . ascii_email ) elif len ( ret . email ) > EMAIL_MAX_LENGTH : reason = __get_length_reason ( ret . email , utf8 = True ) else : reason = "(when converted to IDNA ASCII)" if error == "" : error = "The email address is too long {}." . format ( reason ) if len ( ret . email . encode ( "utf8" ) ) > EMAIL_MAX_LENGTH : if len ( ret . email ) > EMAIL_MAX_LENGTH : reason = __get_length_reason ( ret . email , utf8 = True ) else : reason = "(when encoded in bytes)" if error == "" : error = "The email address is too long {}." . format ( reason ) if check_deliverability and not test_environment : deliverability_info = validate_email_deliverability ( ret [ "domain" ] , ret [ "domain_i18n" ] , timeout , dns_resolver ) if "mx" in deliverability_info : ret . mx = deliverability_info [ "mx" ] ret . mx_fallback_type = deliverability_info [ "mx-fallback" ] if "error" in deliverability_info : if error == "" : error = deliverability_info [ "error" ] ret . error = error if ret . error == "" : ret . valid = True return ret
Validates an email address, returning an ValidationResult object.
Validates an email address returning an ValidationResult object .
"""
    Validates an email address, returning an ValidationResult object. The ValidationResult object's property of valid
    will be set to False if the address is not valid or True when the address is valid. The ValidationResult object's
    property of error will be an empty string if the email in valid or it will contain a string-formatted error message
    if invalid. The email argument can be a str or a bytes instance, but if bytes it must be ASCII-only.
    """ # Allow email to be a str or bytes instance. If bytes, # it must be ASCII because that's how the bytes work # on the wire with SMTP. # At-sign. # Collect return values in this instance. # Validate the email address's local part syntax and get a normalized form. # Validate the email address's domain part syntax and get a normalized form. # Construct the complete normalized form. # If the email address has an ASCII form, add it. # If the email address has an ASCII representation, then we assume it may be # transmitted in ASCII (we can't assume SMTPUTF8 will be used on all hops to # the destination) and the length limit applies to ASCII characters (which is # the same as octets). The number of characters in the internationalized form # may be many fewer (because IDNA ASCII is verbose) and could be less than 254 # Unicode characters, and of course the number of octets over the limit may # not be the number of characters over the limit, so if the email address is # internationalized, we can't give any simple information about why the address # is too long. # # In addition, check that the UTF-8 encoding (i.e. not IDNA ASCII and not # Unicode characters) is at most 254 octets. If the addres is transmitted using # SMTPUTF8, then the length limit probably applies to the UTF-8 encoded octets. # If the email address has an ASCII form that differs from its internationalized # form, I don't think the internationalized form can be longer, and so the ASCII # form length check would be sufficient. If there is no ASCII form, then we have # to check the UTF-8 encoding. The UTF-8 encoding could be up to about four times # longer than the number of characters. # # See the length checks on the local part and the domain. # If there are more than 254 characters, then the ASCII # form is definitely going to be too long. # If there are more than 254 characters, then the UTF-8 # encoding is definitely going to be too long. # Validate the email address's deliverability using DNS # and update the return dict with metadata.
email
allow_smtputf8
allow_empty_local
check_deliverability
test_environment
timeout
dns_resolver