Python,检查是否存在不存在的电子邮件

时间:2019-11-26 09:22:09

标签: python python-3.x email smtp

我是python中的smtp的新手,我想编写一个脚本来检查电子邮件是否有效。

我写了这个脚本,但仍然有错误,例如timeoutError: 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

0 个答案:

没有答案