[英]How to Verify an Email Address in Python Using smtplib
I have been trying to verify an email address entered by the user in my program.我一直在尝试验证用户在我的程序中输入的电子邮件地址。 The code I currently have is:
我目前拥有的代码是:
server = smtplib.SMTP()
server.connect()
server.set_debuglevel(True)
try:
server.verify(email)
except Exception:
return False
finally:
server.quit()
However when I run it I get:但是,当我运行它时,我得到:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
So what I am asking is how do i verify an email address using the smtp module?所以我要问的是如何使用 smtp 模块验证电子邮件地址? I want to check whether the email address actually exists.
我想检查电子邮件地址是否确实存在。
Here's a simple way to verify emails.这是验证电子邮件的简单方法。 This is minimally modified code from this link .
这是来自此链接的最少修改代码。 The first part will check if the email address is well-formed, the second part will ping the SMTP server with that address and see if it gets a success code (250) back or not.
第一部分将检查电子邮件地址是否格式正确,第二部分将使用该地址 ping SMTP 服务器并查看它是否返回成功代码 (250)。 That being said, this isn't failsafe -- depending how this is set up sometimes every email will be returned as valid.
话虽如此,这不是故障安全的——取决于这是如何设置的,有时每封电子邮件都会被返回为有效的。 So you should still send a verification email.
所以你仍然应该发送一封验证邮件。
email_address = 'example@example.com'
#Step 1: Check email
#Check using Regex that an email meets minimum requirements, throw an error if not
addressToVerify = email_address
match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', addressToVerify)
if match == None:
print('Bad Syntax in ' + addressToVerify)
raise ValueError('Bad Syntax')
#Step 2: Getting MX record
#Pull domain name from email address
domain_name = email_address.split('@')[1]
#get the MX record for the domain
records = dns.resolver.query(domain_name, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
#Step 3: 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('me@domain.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()
# Assume 250 as Success
if code == 250:
print('Y')
else:
print('N')
The server name isn't defined properly along with the port.服务器名称未与端口一起正确定义。 Depending on how you have your SMTP server you might need to use the login function.
根据您拥有 SMTP 服务器的方式,您可能需要使用登录功能。
server = smtplib.SMTP(str(SERVER), int(SMTP_PORT))
server.connect()
server.set_debuglevel(True)
try:
server.verify(email)
except Exception:
return False
finally:
server.quit()
you need to specify the smtp host (server) in the SMTP
construct.您需要在
SMTP
构造中指定 smtp 主机(服务器)。 This depends on the email domain.这取决于电子邮件域。 eg for a gmail address you would need something like
gmail-smtp-in.l.google.com
.例如,对于 gmail 地址,您需要类似
gmail-smtp-in.l.google.com
。
The server.verify
which is a SMTP VRFY
probably isnt what you want though. server.verify
,作为 SMTP VRFY
的server.verify
可能不是您想要的。 Most servers disable it.大多数服务器禁用它。
You might want to look at a service like Real Email which has guide for python.你可能想看看像Real Email这样的服务,它有 python 指南。 How to Validate Email Address in python .
如何在 python 中验证电子邮件地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.