Skip to content

Commit f492963

Browse files
committed
Add support for timeout specification
1 parent c047269 commit f492963

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

email_validator/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def validate_email(
5555
email,
5656
allow_smtputf8=True,
5757
allow_empty_local=False,
58-
check_deliverability=True):
58+
check_deliverability=True,
59+
timeout=0):
5960

6061
"""Validates an email address, raising an EmailNotValidError if the address is not valid or returning a dict of information
6162
when the address is valid. The email argument can be a str or a bytes instance, but if bytes it must be ASCII-only."""
@@ -88,7 +89,7 @@ def validate_email(
8889
if check_deliverability:
8990
# Validate the email address's deliverability and update the
9091
# return dict with metadata.
91-
ret.update(validate_email_deliverability(ret["domain"], ret["domain_i18n"]))
92+
ret.update(validate_email_deliverability(ret["domain"], ret["domain_i18n"], timeout))
9293

9394
# If the email address has an ASCII form, add it.
9495
ret["email"] = ret["local"] + "@" + ret["domain_i18n"]
@@ -233,13 +234,16 @@ def validate_email_domain_part(domain):
233234
"domain_i18n": domain_i18n,
234235
}
235236

236-
def validate_email_deliverability(domain, domain_i18n):
237+
def validate_email_deliverability(domain, domain_i18n, timeout=0):
237238
# Check that the domain resolves to an MX record. If there is no MX record,
238239
# try an A or AAAA record which is a deprecated fallback for deliverability.
239240

240241
try:
241242
resolver = dns.resolver.get_default_resolver()
242243

244+
if timeout:
245+
resolver.lifetime = timeout
246+
243247
try:
244248
# Try resolving for MX records and get them in sorted priority order.
245249
response = dns.resolver.query(domain, "MX")
@@ -260,7 +264,7 @@ def validate_email_deliverability(domain, domain_i18n):
260264
mtas = [(0, str(r)) for r in response]
261265
mx_fallback = "AAAA"
262266
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
263-
267+
264268
# If there was no MX, A, or AAAA record, then mail to
265269
# this domain is not deliverable.
266270
raise EmailUndeliverableError("The domain name %s does not exist." % domain_i18n)

0 commit comments

Comments
 (0)