apeye.email_validator

Email address validation functions.

New in version 1.0.0.

This module is a subset of https://pypi.org/project/email-validator

Exceptions:

EmailSyntaxError

Exception raised when an email address fails validation because of its form.

Classes:

ValidatedEmail(​original_email, email, …[, …])

Represents the return type of the validate_email() function.

Functions:

validate_email(​email[, allow_smtputf8, …])

Validates an email address.

validate_email_domain_part(​domain)

Validate the domain part of an email address (the part after the @-sign).

validate_email_local_part(​local[, …])

Validates the local part of an email address (the part before the @-sign).

exception EmailSyntaxError[source]

Bases: ValueError

Exception raised when an email address fails validation because of its form.

class ValidatedEmail(original_email, email, local_part, domain, *, ascii_email=None, ascii_local_part=None, ascii_domain=None, smtputf8=None)[source]

Bases: object

Represents the return type of the validate_email() function.

This class holds the normalized form of the email address alongside other information.

Parameters
  • original_email (str) – The original, unnormalized email address.

  • email (str) – The normalized email address, which should always be used in preference to the original address.

  • local_part (str) – The local part of the email address after Unicode normalization.

  • domain (str) – The domain part of the email address after Unicode normalization or conversion to Unicode from IDNA ascii.

  • ascii_email (Optional[str]) – If not None, a form of the email address that uses 7-bit ASCII characters only. Default None.

  • ascii_local_part (Optional[str]) – If not None, the local part of the email address using 7-bit ASCII characters only. Default None.

  • ascii_domain (Optional[str]) – If not None, a form of the domain name that uses 7-bit ASCII characters only. Default None.

  • smtputf8 (Optional[bool]) – Indicates whether SMTPUTF8 will be required to transmit messages to this address. Default None.

Methods:

__eq__(​other)

Return self == other.

__repr__(​)

Return a string representation of the ValidatedEmail.

__str__(​)

Return str(self).

as_dict(​)

Convenience method for accessing the ValidatedEmail as a dict.

Attributes:

ascii_domain

If not None, a form of the domain name that uses 7-bit ASCII characters only.

ascii_email

If not None, a form of the email address that uses 7-bit ASCII characters only.

ascii_local_part

If not None, the local part of the email address using 7-bit ASCII characters only.

domain

The domain part of the email address after Unicode normalization or conversion to Unicode from IDNA ascii.

email

The normalized email address, which should always be used in preference to the original address.

local_part

The local part of the email address after Unicode normalization.

original_email

The email address that was passed to validate_email.

smtputf8

If True, the SMTPUTF8 feature of your mail relay will be required to transmit messages to this address.

__eq__(other)[source]

Return self == other.

Return type

bool

__repr__()[source]

Return a string representation of the ValidatedEmail.

Return type

str

__str__()[source]

Return str(self).

Return type

str

as_dict()[source]

Convenience method for accessing the ValidatedEmail as a dict.

Return type

Dict[str, Any]

ascii_domain

Type:    Optional[str]

If not None, a form of the domain name that uses 7-bit ASCII characters only.

ascii_email

Type:    Optional[str]

If not None, a form of the email address that uses 7-bit ASCII characters only.

ascii_local_part

Type:    Optional[str]

If not None, the local part of the email address using 7-bit ASCII characters only.

domain

Type:    str

The domain part of the email address after Unicode normalization or conversion to Unicode from IDNA ascii.

email

Type:    str

The normalized email address, which should always be used in preference to the original address.

The normalized address converts an IDNA ASCII domain name to Unicode, if possible, and performs Unicode normalization on the local part and on the domain (if originally Unicode). It is the concatenation of the local_part and domain attributes, separated by an @-sign.

local_part

Type:    str

The local part of the email address after Unicode normalization.

original_email

Type:    str

The email address that was passed to validate_email. (If passed as bytes, this will be a string.)

smtputf8

Type:    Optional[bool]

If True, the SMTPUTF8 feature of your mail relay will be required to transmit messages to this address.

This flag is True when ascii_local_part is missing. Otherwise it is False.

validate_email(email, allow_smtputf8=True, allow_empty_local=False)[source]

Validates an email address.

Parameters
  • email (Union[str, bytes]) – Either a string, or ASCII-encoded bytes.

  • allow_smtputf8 (bool) – Default True.

  • allow_empty_local (bool) – Whether to allow the local part (the bit before the @-sign) to be empty. Default False.

Raises

EmailSyntaxError – if the address is not valid

Return type

ValidatedEmail

validate_email_domain_part(domain)[source]

Validate the domain part of an email address (the part after the @-sign).

Parameters

domain (str)

Return type

Dict[str, str]

validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=False)[source]

Validates the local part of an email address (the part before the @-sign).

Parameters
  • local (str)

  • allow_smtputf8 (bool) – Default True.

  • allow_empty_local (bool) – Whether to allow the local part to be empty/. Default False.

Return type

Dict[str, Any]