You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-20
Original file line number
Diff line number
Diff line change
@@ -43,11 +43,11 @@ pip install email-validator
43
43
44
44
`pip3` also works.
45
45
46
-
Usage
47
-
-----
46
+
Quick Start
47
+
-----------
48
48
49
49
If you're validating a user's email address before creating a user
50
-
account, you might do this:
50
+
account in your application, you might do this:
51
51
52
52
```python
53
53
from email_validator import validate_email, EmailNotValidError
@@ -66,28 +66,18 @@ except EmailNotValidError as e:
66
66
```
67
67
68
68
This validates the address and gives you its normalized form. You should
69
-
put the normalized form in your database and always normalize before
69
+
**put the normalized form in your database** and always normalize before
70
70
checking if an address is in your database.
71
71
72
-
When validating many email addresses or to control the timeout (the default is 15 seconds), create a caching [dns.resolver.Resolver](https://dnspython.readthedocs.io/en/latest/resolver-class.html) to reuse in each call:
73
-
74
-
```python
75
-
from email_validator import validate_email, caching_resolver
The validator will accept internationalized email addresses, but not all
84
73
mail systems can send email to an addresses with non-English characters in
85
74
the *local* part of the address (before the @-sign). See the `allow_smtputf8`
86
75
option below.
87
76
77
+
Usage
78
+
-----
88
79
89
-
Overview
90
-
--------
80
+
### Overview
91
81
92
82
The module provides a function `validate_email(email_address)` which
93
83
takes an email address (either a `str` or `bytes`, but only non-internationalized
@@ -123,8 +113,10 @@ can bounce mail after a delay, and bounced mail may indicate a temporary
123
113
failure of a good email address (sometimes an intentional failure, like
124
114
greylisting). (A/AAAA-record fallback is also checked.)
125
115
126
-
The function also accepts the following keyword arguments (default as
127
-
shown):
116
+
### Options
117
+
118
+
The `validate_email` function also accepts the following keyword arguments
119
+
(defaults are as shown below):
128
120
129
121
`allow_smtputf8=True`: Set to `False` to prohibit internationalized addresses that would
130
122
require the
@@ -137,7 +129,34 @@ shown):
137
129
138
130
`dns_resolver=None`: Pass an instance of [dns.resolver.Resolver](https://dnspython.readthedocs.io/en/latest/resolver-class.html) to control the DNS resolver including setting a timeout and [a cache](https://dnspython.readthedocs.io/en/latest/resolver-caching.html). The `caching_resolver` function shown above is a helper function to construct a dns.resolver.Resolver with a [LRUCache](https://dnspython.readthedocs.io/en/latest/resolver-caching.html#dns.resolver.LRUCache). Reuse the same resolver instance across calls to `validate_email` to make use of the cache.
139
131
140
-
In non-production test environments, you may want to allow `@test` or `@mycompany.test` email addresses to be used as placeholder email addresses, which would normally not be permitted. In that case, pass `test_environment=True`. DNS-based deliverability checks will be disabled as well. Other [Special Use Domain Names](https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml) are always considered invalid and raise `EmailUndeliverableError`.
132
+
`test_environment=False`: DNS-based deliverability checks are disabled and `test` and `subdomain.test` domain names are permitted (see below).
133
+
134
+
### DNS timeout and cache
135
+
136
+
When validating many email addresses or to control the timeout (the default is 15 seconds), create a caching [dns.resolver.Resolver](https://dnspython.readthedocs.io/en/latest/resolver-class.html) to reuse in each call. The `caching_resolver` function returns one easily for you:
137
+
138
+
```python
139
+
from email_validator import validate_email, caching_resolver
This library rejects email addresess that use the [Special Use Domain Names](https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml)`invalid`, `localhost`, `test`, and some others by raising `EmailUndeliverableError`. This is to protect your system from abuse: You probably don't want a user to be able to cause an email to be sent to `localhost`. However, in your non-production test environments you may want to use `@test` or `@myname.test` email addresses. There are two ways you can allow this:
150
+
151
+
A. Add `test_environment=True` to the call to `validate_email` (see above).
152
+
B. Remove the special-use domain name that you want to use from `email_validator.SPECIAL_USE_DOMAIN_NAMES`:
It is tempting to use `@example.com/net/org` in tests. These domains are reserved to IANA for use in documentation so there is no risk of accidentally emailing someone at those domains. But beware that this library will reject these domain names if DNS-based deliverability checks are not disabled because these domains do not resolve to domains that accept email. In tests, consider using your own domain name or `@test` or `@myname.test` instead.
0 commit comments