简体   繁体   English

通过 Python 进行 DNS 验证电子邮件记录

[英]DNS Verifying email records via Python

First off.. excuse my complete noobness when it comes to DNS protocols.首先......请原谅我在 DNS 协议方面的完全菜鸟。

Im trying to use python to verify if email exists (using this code as base)我试图使用 python 来验证电子邮件是否存在(使用此代码作为基础)

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这在 Digitalocean VPS 服务器上运行良好

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.所以我认为这是一个端口问题,但是我的服务器上没有打开端口 25。

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.所以我取了 server.local_hostname 的字符串值(顺便说一下,这不是我的域……而且我还没有配置 STMP ……而且正如我之前提到的,我没有打开端口25)并在我的本地机器上运行它,我仍然收到超时错误。

1) There's something I must not be understanding when it comes to DNS protocol.. any ideas? 1)当涉及到 DNS 协议时,我一定不能理解某些东西......有什么想法吗? 2) If there's some DNS protocol happening between SMTP servers, is it possible to use a proxy server to validate email? 2) 如果 SMTP 服务器之间发生了一些 DNS 协议,是否可以使用代理服务器来验证电子邮件?

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.当然,没有什么神奇的 DNS 记录可以告诉您某个特定的电子邮件地址是否存在。 That would be simply inviting spam from people who scrape DNS records to spam people.这只是将垃圾邮件从抓取 DNS 记录的人那里邀请给垃圾邮件的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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