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
# After this point, use only the normalized form of the email address,
71
+
# especially before going to a database query.
71
72
email = validation.email
73
+
72
74
except EmailNotValidError as e:
73
-
# Email is not valid.
74
-
# The exception message is human-readable.
75
+
76
+
# The exception message is human-readable explanation of why it's
77
+
# not a valid (or deliverable) email address.
75
78
print(str(e))
76
79
```
77
80
@@ -108,12 +111,15 @@ that no one uses anymore even though they are still valid and deliverable, since
108
111
they will probably give you grief if you're using email for login. (See
109
112
later in the document about how to allow some obsolete forms.)
110
113
111
-
The validator checks that the domain name in the email address has a
112
-
DNS MX record (except a NULL MX record) indicating that it can receive
113
-
email (or a fallback A-record, see below).
114
-
There is nothing to be gained by trying to actually contact an SMTP
115
-
server, so that's not done here. For privacy, security, and practicality
116
-
reasons servers are good at not giving away whether an address is
114
+
The validator optionally checks that the domain name in the email address has
115
+
a DNS MX record indicating that it can receive email. (Except a Null MX record.
116
+
If there is no MX record, a fallback A/AAAA-record is permitted, unless
117
+
a reject-all SPF record is present.) DNS is slow and sometimes unavailable or
118
+
unreliable, so consider whether these checks are useful for your use case and
119
+
turn them off if they aren't.
120
+
There is nothing to be gained by trying to actually contact an SMTP server, so
121
+
that's not done here. For privacy, security, and practicality reasons, servers
122
+
are good at not giving away whether an address is
117
123
deliverable or not: email addresses that appear to accept mail at first
118
124
can bounce mail after a delay, and bounced mail may indicate a temporary
119
125
failure of a good email address (sometimes an intentional failure, like
@@ -124,11 +130,11 @@ greylisting).
124
130
The `validate_email` function also accepts the following keyword arguments
125
131
(defaults are as shown below):
126
132
127
-
`check_deliverability=True`: If true, a DNS query is made to check that a non-null MX record is present for the domain-part of the email address (or if not, an A/AAAA record as an MX fallback can be present but in that case a reject-all SPF record must not be present). Set to `False` to skip this DNS-based check. DNS is slow and sometimes unavailable, so consider whether these checks are useful for your use case. It is recommended to pass `False` when performing validation for login pages (but not account creation pages) since re-validation of a previously validated domain in your database by querying DNS at every login is probably undesirable. You can also set `email_validator.CHECK_DELIVERABILITY` to `False` to turn this off for all calls by default.
133
+
`check_deliverability=True`: If true, DNS queries are made to check that the domain name in the email address (the part after the @-sign) can receive mail, as described above. Set to `False` to skip this DNS-based check. It is recommended to pass `False` when performing validation for login pages (but not account creation pages) since re-validation of a previously validated domain in your database by querying DNS at every login is probably undesirable. You can also set `email_validator.CHECK_DELIVERABILITY` to `False` to turn this off for all calls by default.
128
134
129
-
`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.
135
+
`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 below 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.
130
136
131
-
`test_environment=False`: DNS-based deliverability checks are disabled and `test` and `subdomain.test` domain names are permitted (see below). You can also set `email_validator.TEST_ENVIRONMENT` to `True` to turn it on for all calls by default.
137
+
`test_environment=False`: If `True`, DNS-based deliverability checks are disabled and `test` and `**.test` domain names are permitted (see below). You can also set `email_validator.TEST_ENVIRONMENT` to `True` to turn it on for all calls by default.
132
138
133
139
`allow_smtputf8=True`: Set to `False` to prohibit internationalized addresses that would
134
140
require the
@@ -160,15 +166,15 @@ while True:
160
166
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 `EmailSyntaxError`. 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 three ways you can allow this:
161
167
162
168
1. Add `test_environment=True` to the call to `validate_email` (see above).
163
-
2. Set `email_validator.TEST_ENVIRONMENT` to `True`.
164
-
3. Remove the special-use domain name that you want to use from `email_validator.SPECIAL_USE_DOMAIN_NAMES`:
169
+
2. Set `email_validator.TEST_ENVIRONMENT` to `True` globally.
170
+
3. Remove the special-use domain name that you want to use from `email_validator.SPECIAL_USE_DOMAIN_NAMES`, e.g.:
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.
177
+
It is tempting to use `@example.com/net/org` in tests. They are *not* in this library's `SPECIAL_USE_DOMAIN_NAMES` list so you can, but shouldn't, use them. 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 nevertheless 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.
172
178
173
179
Internationalized email addresses
174
180
---------------------------------
@@ -255,17 +261,23 @@ change the user's login information without telling them.)
255
261
Normalization
256
262
-------------
257
263
264
+
### Unicode Normalization
265
+
258
266
The use of Unicode in email addresses introduced a normalization
259
267
problem. Different Unicode strings can look identical and have the same
260
268
semantic meaning to the user. The `email` field returned on successful
261
269
validation provides the correctly normalized form of the given email
262
-
address:
270
+
address.
271
+
272
+
For example, the CJK fullwidth Latin letters are considered semantically
273
+
equivalent in domain names to their ASCII counterparts. This library
274
+
normalizes them to their ASCII counterparts:
263
275
264
276
```python
265
277
valid = validate_email("me@Domain.com")
266
-
email =valid.ascii_email
267
-
print(email)
268
-
# prints: me@domain.com
278
+
print(valid.email)
279
+
print(valid.ascii_email)
280
+
# prints "me@domain.com" twice
269
281
```
270
282
271
283
Because an end-user might type their email address in different (but
@@ -292,6 +304,8 @@ and conversion from Punycode to Unicode characters.
292
304
3.1](https://tools.ietf.org/html/rfc6532#section-3.1) and [RFC 5895
raiseEmailSyntaxError("The email address contains invalid characters before the @-sign: "+", ".join(sorted(bad_chars)) +".")
195
195
196
196
# Check for dot errors imposted by the dot-atom rule.
197
-
# (RFC 2822 3.2.4)
197
+
# (RFC 5322 3.2.3)
198
198
check_dot_atom(local, 'An email address cannot start with a {}.', 'An email address cannot have a {} immediately before the @-sign.', is_hostname=False)
199
199
200
200
# All of the reasons should already have been checked, but just in case
Copy file name to clipboardExpand all lines: tests/test_syntax.py
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -306,6 +306,7 @@ def test_domain_literal():
306
306
('my\n@example.com', 'The email address contains invalid characters before the @-sign: U+000A.'),
307
307
('test@\n', 'The part after the @-sign contains invalid characters: U+000A.'),
308
308
('bad"quotes"@example.com', 'The email address contains invalid characters before the @-sign: \'"\'.'),
309
+
('obsolete."quoted".atom@example.com', 'The email address contains invalid characters before the @-sign: \'"\'.'),
309
310
('11111111112222222222333333333344444444445555555555666666666677777@example.com', 'The email address is too long before the @-sign (1 character too many).'),
310
311
('111111111122222222223333333333444444444455555555556666666666777777@example.com', 'The email address is too long before the @-sign (2 characters too many).'),
311
312
('me@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444444444455555555556.com', 'The email address is too long (4 characters too many).'),
0 commit comments