Skip to content

Commit f16b1e6

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 50e82a6 commit f16b1e6

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ while True:
165165

166166
### Test addresses
167167

168-
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:
168+
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:
169169

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

email_validator/__init__.py

Lines changed: 4 additions & 6 deletions
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

tests/test_main.py

Lines changed: 1 addition & 1 deletion
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)