Given an email address, and some options, returns a ValidatedEmail instance with information about the address if it is valid or, if the address is not valid, raises an EmailNotValidError. This is the main function of the module.
(
email: Union[str, bytes],
/, # prior arguments are positional-only
*, # subsequent arguments are keyword-only
allow_smtputf8: Optional[bool] = None,
allow_empty_local: Optional[bool] = None,
allow_quoted_local: Optional[bool] = None,
allow_domain_literal: Optional[bool] = None,
allow_display_name: Optional[bool] = None,
strict: Optional[bool] = None,
check_deliverability: Optional[bool] = None,
test_environment: Optional[bool] = None,
globally_deliverable: Optional[bool] = None,
timeout: Optional[int] = None,
dns_resolver: Optional[_Resolver] = None
)
| 14 | |
| 15 | |
| 16 | def validate_email( |
| 17 | email: Union[str, bytes], |
| 18 | /, # prior arguments are positional-only |
| 19 | *, # subsequent arguments are keyword-only |
| 20 | allow_smtputf8: Optional[bool] = None, |
| 21 | allow_empty_local: Optional[bool] = None, |
| 22 | allow_quoted_local: Optional[bool] = None, |
| 23 | allow_domain_literal: Optional[bool] = None, |
| 24 | allow_display_name: Optional[bool] = None, |
| 25 | strict: Optional[bool] = None, |
| 26 | check_deliverability: Optional[bool] = None, |
| 27 | test_environment: Optional[bool] = None, |
| 28 | globally_deliverable: Optional[bool] = None, |
| 29 | timeout: Optional[int] = None, |
| 30 | dns_resolver: Optional[_Resolver] = None |
| 31 | ) -> ValidatedEmail: |
| 32 | """ |
| 33 | Given an email address, and some options, returns a ValidatedEmail instance |
| 34 | with information about the address if it is valid or, if the address is not |
| 35 | valid, raises an EmailNotValidError. This is the main function of the module. |
| 36 | """ |
| 37 | |
| 38 | # Fill in default values of arguments. |
| 39 | from . import ALLOW_SMTPUTF8, ALLOW_EMPTY_LOCAL, ALLOW_QUOTED_LOCAL, ALLOW_DOMAIN_LITERAL, ALLOW_DISPLAY_NAME, \ |
| 40 | STRICT, GLOBALLY_DELIVERABLE, CHECK_DELIVERABILITY, TEST_ENVIRONMENT, DEFAULT_TIMEOUT |
| 41 | if allow_smtputf8 is None: |
| 42 | allow_smtputf8 = ALLOW_SMTPUTF8 |
| 43 | if allow_empty_local is None: |
| 44 | allow_empty_local = ALLOW_EMPTY_LOCAL |
| 45 | if allow_quoted_local is None: |
| 46 | allow_quoted_local = ALLOW_QUOTED_LOCAL |
| 47 | if allow_domain_literal is None: |
| 48 | allow_domain_literal = ALLOW_DOMAIN_LITERAL |
| 49 | if allow_display_name is None: |
| 50 | allow_display_name = ALLOW_DISPLAY_NAME |
| 51 | if strict is None: |
| 52 | strict = STRICT |
| 53 | if check_deliverability is None: |
| 54 | check_deliverability = CHECK_DELIVERABILITY |
| 55 | if test_environment is None: |
| 56 | test_environment = TEST_ENVIRONMENT |
| 57 | if globally_deliverable is None: |
| 58 | globally_deliverable = GLOBALLY_DELIVERABLE |
| 59 | if timeout is None and dns_resolver is None: |
| 60 | timeout = DEFAULT_TIMEOUT |
| 61 | |
| 62 | if isinstance(email, str): |
| 63 | pass |
| 64 | elif isinstance(email, bytes): |
| 65 | # Allow email to be a bytes instance as if it is what |
| 66 | # will be transmitted on the wire. But assume SMTPUTF8 |
| 67 | # is unavailable, so it must be ASCII. |
| 68 | try: |
| 69 | email = email.decode("ascii") |
| 70 | except ValueError as e: |
| 71 | raise EmailSyntaxError("The email address is not valid ASCII.") from e |
| 72 | else: |
| 73 | raise TypeError("email must be str or bytes") |