Skip to content

Commit 91aa260

Browse files
committed
Document how to modify SPECIAL_USE_DOMAIN_NAMES
1 parent 9d7d02a commit 91aa260

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

Diff for: README.md

+39-20
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ pip install email-validator
4343

4444
`pip3` also works.
4545

46-
Usage
47-
-----
46+
Quick Start
47+
-----------
4848

4949
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:
5151

5252
```python
5353
from email_validator import validate_email, EmailNotValidError
@@ -66,28 +66,18 @@ except EmailNotValidError as e:
6666
```
6767

6868
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
7070
checking if an address is in your database.
7171

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
76-
77-
resolver = caching_resolver(timeout=10)
78-
79-
while True:
80-
email = validate_email(email, dns_resolver=resolver).email
81-
```
82-
8372
The validator will accept internationalized email addresses, but not all
8473
mail systems can send email to an addresses with non-English characters in
8574
the *local* part of the address (before the @-sign). See the `allow_smtputf8`
8675
option below.
8776

77+
Usage
78+
-----
8879

89-
Overview
90-
--------
80+
### Overview
9181

9282
The module provides a function `validate_email(email_address)` which
9383
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
123113
failure of a good email address (sometimes an intentional failure, like
124114
greylisting). (A/AAAA-record fallback is also checked.)
125115

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):
128120

129121
`allow_smtputf8=True`: Set to `False` to prohibit internationalized addresses that would
130122
require the
@@ -137,7 +129,34 @@ shown):
137129

138130
`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.
139131

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
140+
141+
resolver = caching_resolver(timeout=10)
142+
143+
while True:
144+
email = validate_email(email, dns_resolver=resolver).email
145+
```
146+
147+
### Test addresses
148+
149+
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`:
153+
154+
```python
155+
import email_validator
156+
email_validator.SPECIAL_USE_DOMAIN_NAMES.remove("test")
157+
```
158+
159+
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.
141160

142161
Internationalized email addresses
143162
---------------------------------

Diff for: email_validator/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
# The domain names without dots would be caught by the check that the domain
4444
# name in an email address must have a period, but this list will also catch
4545
# subdomains of these domains, which are also reserved.
46-
SPECIAL_USE_DOMAIN_NAMES = (
46+
SPECIAL_USE_DOMAIN_NAMES = [
4747
# The "arpa" entry here is consolidated from a lot of arpa subdomains
4848
# for private address (i.e. non-routable IP addresses like 172.16.x.x)
4949
# reverse mapping, plus some other subdomains. Although RFC 6761 says
@@ -99,7 +99,7 @@
9999
# in application-level test environments. These domains will generally
100100
# fail deliverability checks because "test" is not an actual TLD.
101101
"test",
102-
)
102+
]
103103

104104
# ease compatibility in type checking
105105
if sys.version_info >= (3,):

0 commit comments

Comments
 (0)