import DNS, smtplib
DNS.DiscoverNameServers()
mx_hosts = DNS.mxlookup(hostname)
for mx in mx_hosts:
    smtp = smtplib.SMTP()
try:
    smtp.connect(mx[1])
except smtplib.SMTPConnectError:
    continue
How do I validate the MX record for a domain in python?