简体   繁体   中英

DNS Verifying email records via Python

First off.. excuse my complete noobness when it comes to DNS protocols.

Im trying to use python to verify if email exists (using this code as base)

import re
import smtplib
import dns.resolver

# Address used for SMTP MAIL FROM command  
fromAddress = 'corn@bt.com'

# Simple Regex for syntax checking
regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'

# Email address to verify
inputAddress = input('Please enter the emailAddress to verify:')
addressToVerify = str(inputAddress)

# Syntax check
match = re.match(regex, addressToVerify)
if match == None:
    print('Bad Syntax')
    raise ValueError('Bad Syntax')

# Get domain for DNS lookup
splitAddress = addressToVerify.split('@')
domain = str(splitAddress[1])
print('Domain:', domain)

# MX record lookup
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)


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

# SMTP Conversation
server.connect(mxRecord)
server.helo(server.local_hostname) ### server.local_hostname(Get local server hostname)
server.mail(fromAddress)
code, message = server.rcpt(str(addressToVerify))
server.quit()

#print(code)
#print(message)

# Assume SMTP response 250 is success
if code == 250:
    print('Success')
else:
    print('Bad')

This works fine on a Digitalocean VPS server

But this code doesnt work on local machine (timeout error). So I thought it's a port issue, however port 25 isn't open on my server.

So I took the string value of server.local_hostname (which , by the way.. isn't my domain.. AND i haven't config'd an STMP .. AND as I mentioned before, I haven't opened up port 25) and run it on my local machine, i still get time out error.

1) There's something I must not be understanding when it comes to DNS protocol.. any ideas? 2) If there's some DNS protocol happening between SMTP servers, is it possible to use a proxy server to validate email?

It is impossible to verify that an e-mail address exists.

Plenty of servers will accept mail to any address and discard from there. Many addresses are aliases for others. Certainly, there isn't some magic DNS record that tells you whether or not a particular e-mail address exists. That would be simply inviting spam from people who scrape DNS records to spam people.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM