Fixing connection error
This commit is contained in:
parent
03a7a75600
commit
9ea2bb5cf0
|
@ -1,5 +1,5 @@
|
|||
from re import compile as re_compile
|
||||
from smtplib import SMTP
|
||||
from smtplib import SMTP, SMTPServerDisconnected
|
||||
from socket import gethostname
|
||||
from typing import Optional
|
||||
|
||||
|
@ -27,7 +27,12 @@ def _get_mx_records(domain: str) -> list:
|
|||
|
||||
def mx_check(
|
||||
email_address: str, from_address: Optional[str] = None,
|
||||
smtp_timeout: int = 10) -> bool:
|
||||
smtp_timeout: int = 10) -> Optional[bool]:
|
||||
"""
|
||||
Return `True` if the host responds with a deliverable response code,
|
||||
`False` if not-deliverable.
|
||||
Also, return `None` if there was an error.
|
||||
"""
|
||||
from_address = from_address or email_address
|
||||
host = gethostname()
|
||||
|
||||
|
@ -45,7 +50,10 @@ def mx_check(
|
|||
smtp.helo(host)
|
||||
smtp.mail(from_address)
|
||||
code, message = smtp.rcpt(email_address)
|
||||
smtp.quit()
|
||||
try:
|
||||
smtp.quit()
|
||||
except SMTPServerDisconnected:
|
||||
return None
|
||||
if code == 250:
|
||||
return True
|
||||
return False
|
||||
|
|
|
@ -80,6 +80,7 @@ class EmailValidator(object):
|
|||
|
||||
if domain_part in self.domain_whitelist:
|
||||
return True
|
||||
print(domain_part, domain_part in self.domain_blacklist)
|
||||
if domain_part in self.domain_blacklist:
|
||||
return False
|
||||
|
||||
|
|
|
@ -7,15 +7,18 @@ from .regex_check import regex_check
|
|||
def validate_email(
|
||||
email_address: str, check_regex: bool = True, check_mx: bool = True,
|
||||
from_address: Optional[str] = None, smtp_timeout: int = 10,
|
||||
use_blacklist: bool = True) -> bool:
|
||||
use_blacklist: bool = True) -> Optional[bool]:
|
||||
"""
|
||||
Return `True` or `False` depending if the email address exists
|
||||
or/and can be delivered.
|
||||
|
||||
Return `None` if the result is ambigious.
|
||||
"""
|
||||
if check_regex and not regex_check(
|
||||
value=email_address, use_blacklist=use_blacklist):
|
||||
return False
|
||||
|
||||
if check_mx and not mx_check(
|
||||
email_address, from_address=from_address,
|
||||
smtp_timeout=smtp_timeout):
|
||||
return False
|
||||
|
||||
return True
|
||||
if not check_mx:
|
||||
return True
|
||||
return mx_check(
|
||||
email_address=email_address, from_address=from_address,
|
||||
smtp_timeout=smtp_timeout)
|
||||
|
|
Loading…
Reference in New Issue