是否有一个好方法来检查表单输入使用正则表达式,以确保它是一个正确的风格的电子邮件地址?从昨晚开始搜索,每个人都回答了关于这个话题的问题,如果它是一个子域名的电子邮件地址,似乎也有问题。
当前回答
我还没有在这里的一堆自定义正则表达式答案中看到答案,但是……
存在一个名为py3-validate-email validate_email的python库,它有3个级别的电子邮件验证,包括询问有效的SMTP服务器电子邮件地址是否有效(不发送电子邮件)。
安装
python -m pip install py3-validate-email
基本用法:
from validate_email import validate_email
is_valid = validate_email(email_address='example@example.com', \
check_regex=True, check_mx=True, \
from_address='my@from.addr.ess', helo_host='my.host.name', \
smtp_timeout=10, dns_timeout=10, use_blacklist=True)
对于那些对肮脏细节感兴趣的人,validate_email.py (source)旨在忠实于RFC 2822。
我们所做的只是将输入字符串与1进行比较 庞大的正则表达式。但是构建那个regexp 通过组装它,可以更容易地确保其正确性 从RFC定义的“令牌”。每一个符号都是 在附带的单元测试文件中测试。
你可能需要pyDNS模块来检查SMTP服务器
pip install pyDNS
或者来自Ubuntu
apt-get install python3-dns
其他回答
检查电子邮件使用email_validator
from email_validator import validate_email, EmailNotValidError
def check_email(email):
try:
v = validate_email(email) # validate and get info
email = v["email"] # replace with normalized form
print("True")
except EmailNotValidError as e:
# email is not valid, exception message is human-readable
print(str(e))
check_email("test@gmailcom")
我发现了一个很好的(经过测试的)方法来检查有效的电子邮件地址。我把代码粘贴在这里:
# here i import the module that implements regular expressions
import re
# here is my function to check for valid email address
def test_email(your_pattern):
pattern = re.compile(your_pattern)
# here is an example list of email to check it at the end
emails = ["john@example.com", "python-list@python.org", "wha.t.`1an?ug{}ly@email.com"]
for email in emails:
if not re.match(pattern, email):
print "You failed to match %s" % (email)
elif not your_pattern:
print "Forgot to enter a pattern!"
else:
print "Pass"
# my pattern that is passed as argument in my function is here!
pattern = r"\"?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)\"?"
# here i test my function passing my pattern
test_email(pattern)
The only really accurate way of distinguishing real, valid email addresses from invalid ones is to send mail to it. What counts as an email is surprisingly convoluted ("John Doe" <john.doe@example.com>" actually is a valid email address), and you most likely want the email address to actually send mail to it later. After it passes some basic sanity checks (such as in Thomas's answer, has an @ and at least one . after the @), you should probably just send an email verification letter to the address, and wait for the user to follow a link embedded in the message to confirm that the email was valid.
电子邮件地址非常复杂。下面是一个匹配每个rfc822有效地址的示例正则表达式: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
您会注意到它可能比程序的其他部分要长。甚至还有用于验证电子邮件地址的Perl完整模块。所以你可能不会得到任何东西,100%完美的正则表达式,同时也可读。下面是一个递归下降解析器示例: http://cpansearch.perl.org/src/ABIGAIL/RFC-RFC822-Address-2009110702/lib/RFC/RFC822/Address.pm
但是您需要决定是需要完美的解析还是简单的代码。
Python标准库附带了一个电子邮件解析函数:email.utils.parseaddr()。
它返回一个包含电子邮件的真实姓名和实际地址部分的二元组:
>>> from email.utils import parseaddr
>>> parseaddr('foo@example.com')
('', 'foo@example.com')
>>> parseaddr('Full Name <full@example.com>')
('Full Name', 'full@example.com')
>>> parseaddr('"Full Name with quotes and <weird@chars.com>" <weird@example.com>')
('Full Name with quotes and <weird@chars.com>', 'weird@example.com')
如果解析不成功,它返回一个空字符串的二元组:
>>> parseaddr('[invalid!email]')
('', '')
这个解析器的一个问题是,它接受任何被认为是RFC-822和朋友的有效电子邮件地址的东西,包括许多在广泛的互联网上显然无法寻址的东西:
>>> parseaddr('invalid@example,com') # notice the comma
('', 'invalid@example')
>>> parseaddr('invalid-email')
('', 'invalid-email')
因此,正如@TokenMacGuy所说,检查电子邮件地址的唯一确定方法是向预期的地址发送电子邮件,并等待用户对消息中的信息采取行动。
然而,你可能想要检查,至少,在第二个元组元素上是否存在@-符号,就像@bvukelic建议的那样:
>>> '@' in parseaddr("invalid-email")[1]
False
如果你想更进一步,你可以安装dnspython项目并解析电子邮件域的邮件服务器(“@”后面的部分),只有当有实际的MX服务器时才尝试发送电子邮件:
>>> from dns.resolver import query
>>> domain = 'foo@bar@google.com'.rsplit('@', 1)[-1]
>>> bool(query(domain, 'MX'))
True
>>> query('example.com', 'MX')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
[...]
dns.resolver.NoAnswer
>>> query('not-a-domain', 'MX')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
[...]
dns.resolver.NXDOMAIN
你可以通过捕获dns.exception.DNSException来捕获NoAnswer和NXDOMAIN。
是的,foo@bar@google.com是一个语法有效的地址。应该只考虑最后一个@来检测域部分从哪里开始。