python script to check if a email address is valid.?

Viewed 36

I am trying to validate a email. What i tought of is to basically read the messages connecting to this mailbox. and i have followed this blow https://www.scottbrady91.com/email-verification/python-email-verification-script

Below is my script:

import socket
import smtplib
import dns.resolver


email_address = 'dummy@cisco.com'

#Pull domain name from email address
domain_name = email_address.split('@')[1]

#get the MX record for the domain
my_resolver = dns.resolver.Resolver()
records = my_resolver.resolve(domain_name, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)

# ping email server
#check if the email address exists

# Get local server hostname
host = socket.gethostname()

# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)

# SMTP Conversation
server.connect(mxRecord)
server.helo(host)
server.mail('test@zozo.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()

if code == 250:
    print('Yes')
else:
    print('N')

the thing is every email is returned as valid until the domain is valid any help is appriciated

0 Answers
Related