Skip to content

Commit

Permalink
Fix the returned implicit MX record when there is a fallback
Browse files Browse the repository at this point in the history
The object returned by validate_email returns the queried MX records when deliverability checks are run. When there is an implicit MX record (no MX record but an A or AAAA record), the value is a single entry that points to the host, not a list of the A or AAAA values. SMTP 5321 5.1:

> If an empty list of MXs is returned, the address is treated as if it was associated with an implicit MX R, with a preference of 0, pointing to that host.
  • Loading branch information
JoshData committed Apr 14, 2024
1 parent d6d3d15 commit da48fd1
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
In Development
--------------

* When a domain name has no MX record but does have an A or AAAA record, the mx field in the object returned by validate_email incorrectly held the IP addresses rather than the domain itself.
* Fixes in tests.

2.1.1 (February 26, 2024)
Expand Down
4 changes: 2 additions & 2 deletions email_validator/deliverability.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Option
# If there was no MX record, fall back to an A record. (RFC 5321 Section 5)
try:
response = dns_resolver.resolve(domain, "A")
deliverability_info["mx"] = [(0, str(r)) for r in response]
deliverability_info["mx"] = [(0, domain)]
deliverability_info["mx_fallback_type"] = "A"

except dns.resolver.NoAnswer:
Expand All @@ -69,7 +69,7 @@ def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Option
# (It's unclear if SMTP servers actually do this.)
try:
response = dns_resolver.resolve(domain, "AAAA")
deliverability_info["mx"] = [(0, str(r)) for r in response]
deliverability_info["mx"] = [(0, domain)]
deliverability_info["mx_fallback_type"] = "AAAA"

except dns.resolver.NoAnswer as e:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_deliverability.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'domain,expected_response',
[
('gmail.com', {'mx': [(5, 'gmail-smtp-in.l.google.com'), (10, 'alt1.gmail-smtp-in.l.google.com'), (20, 'alt2.gmail-smtp-in.l.google.com'), (30, 'alt3.gmail-smtp-in.l.google.com'), (40, 'alt4.gmail-smtp-in.l.google.com')], 'mx_fallback_type': None}),
('pages.github.com', {'mx': [(0, '185.199.108.153'), (0, '185.199.109.153'), (0, '185.199.111.153'), (0, '185.199.110.153')], 'mx_fallback_type': 'A'}),
('pages.github.com', {'mx': [(0, 'pages.github.com')], 'mx_fallback_type': 'A'}),
],
)
def test_deliverability_found(domain, expected_response):
Expand Down

0 comments on commit da48fd1

Please sign in to comment.