我是python中的smtp的新手,我想编写一个脚本来检查电子邮件是否有效。
我写了这个脚本,但仍然有错误,例如timeout
或Error: Socket error: 0x06: TTL expired
,但我不知道为什么:/
domaine = email.split('@')[1]
try:
mx_hosts = dns.resolver.query(domaine, 'MX')
except Exception:
list_not_email.append(email)
break
for mx in mx_hosts:
exchange = str(mx.exchange)
# .. if this doesn't raise an exception it is a valid MX host...
try:
smtp = smtplib.SMTP(exchange)
# smtp.connect(exchange)
smtp.helo() # send the HELO command
smtp.mail('webbot92@gmail.com') # send the MAIL command
resp = smtp.rcpt(email)
print(resp)
if resp[0] == 250: # check the status code
print('Email valid')
elif resp[0] == 550:
print('Email not valid')
elif resp[0] == 450:
print('Error')
except smtplib.SMTPConnectError:
continue # try the next MX server in list