Skip to content

Commit a906eb5

Browse files
committed
Change special use domain names to raise EmailSyntaxError instead of EmailUndeliverableError
This way all DNS-based checks raise EmailUndeliverableError and all non-DNS-based checks raise EmailSyntaxError. This was suggested by someone in GitHub issues although I can't find that anymore.
1 parent 2ad9b1b commit a906eb5

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

Diff for: README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ while True:
166166

167167
### Test addresses
168168

169-
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 three ways you can allow this:
169+
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:
170170

171171
1. Add `test_environment=True` to the call to `validate_email` (see above).
172172
2. Set `email_validator.TEST_ENVIRONMENT` to `True`.
@@ -410,11 +410,9 @@ or likely to cause trouble:
410410
(without NULL MX or SPF -all DNS records) if deliverability
411411
checks are turned on.
412412
Most [Special Use Domain Names](https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml)
413-
and their subdomains are considered invalid (except see
414-
the `test_environment` parameter above), if deliverability checks are
415-
turned on. Domain names without a `.` are rejected as a syntax error
416-
since no one has an email address directly at a TLD, and a missing
417-
TLD is a common user error.
413+
and their subdomains and
414+
domain names without a `.` are rejected as a syntax error
415+
(except see the `test_environment` parameter above).
418416
* Obsolete email syntaxes are rejected:
419417
The "quoted string" form of the local part of the email address (RFC
420418
5321 4.1.2) is not permitted.

Diff for: email_validator/__init__.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,7 @@ def validate_email_domain_part(domain, test_environment=False):
559559
if "." not in ascii_domain and not (ascii_domain == "test" and test_environment):
560560
raise EmailSyntaxError("The domain name %s is not valid. It should have a period." % domain_i18n)
561561

562-
# Check special-use and reserved domain names. Raise these as
563-
# deliverability errors since they are syntactically valid.
562+
# Check special-use and reserved domain names.
564563
# Some might fail DNS-based deliverability checks, but that
565564
# can be turned off, so we should fail them all sooner.
566565
for d in SPECIAL_USE_DOMAIN_NAMES:
@@ -569,12 +568,11 @@ def validate_email_domain_part(domain, test_environment=False):
569568
continue
570569

571570
if ascii_domain == d or ascii_domain.endswith("." + d):
572-
raise EmailUndeliverableError("The domain name %s is a special-use or reserved name that cannot be used with email." % domain_i18n)
571+
raise EmailSyntaxError("The domain name %s is a special-use or reserved name that cannot be used with email." % domain_i18n)
573572

574-
# We also know that all TLDs currently end with a letter, and
575-
# we'll consider that a non-DNS based deliverability check.
573+
# We also know that all TLDs currently end with a letter.
576574
if not re.search(r"[A-Za-z]\Z", ascii_domain):
577-
raise EmailUndeliverableError(
575+
raise EmailSyntaxError(
578576
"The domain name %s is not valid. It is not within a valid top-level domain." % domain_i18n
579577
)
580578

Diff for: tests/test_main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def test_email_invalid_syntax(email_input, error_msg):
271271
def test_email_invalid_reserved_domain(email_input):
272272
# Since these all fail deliverabiltiy from a static list,
273273
# DNS deliverability checks do not arise.
274-
with pytest.raises(EmailUndeliverableError) as exc_info:
274+
with pytest.raises(EmailSyntaxError) as exc_info:
275275
validate_email(email_input)
276276
# print(f'({email_input!r}, {str(exc_info.value)!r}),')
277277
assert "is a special-use or reserved name" in str(exc_info.value)

0 commit comments

Comments
 (0)