Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29767

Python, check if a email exist of not

$
0
0

I'm new with smtp in python, I would like to write a script that check if a email is valid or not.

I write this script but I still have error, like timeout or Error: Socket error: 0x06: TTL expired and I don't know why :/

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

Viewing all articles
Browse latest Browse all 29767

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>