是否有一个好方法来检查表单输入使用正则表达式,以确保它是一个正确的风格的电子邮件地址?从昨晚开始搜索,每个人都回答了关于这个话题的问题,如果它是一个子域名的电子邮件地址,似乎也有问题。


当前回答

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.

其他回答

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是一个语法有效的地址。应该只考虑最后一个@来检测域部分从哪里开始。

如果你想从一个长字符串或文件中取出邮件,那么试试这个。

([^@|\s]+@[^@]+\.[^@|\s]+)

注意,当你的电子邮件地址前后都有空格时,这是有效的。如果你没有空间或有一些特殊的字符,那么你可以尝试修改它。

工作的例子:

string="Hello ABCD, here is my mail id example@me.com "
res = re.search("([^@|\s]+@[^@]+\.[^@|\s]+)",string,re.I)
res.group(1)

这将从该字符串中删除example@me.com。

另外,注意这可能不是正确答案……但我把它贴在这里是为了帮助像我这样有特殊要求的人

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.

检查电子邮件使用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")

这通常是使用正则表达式解决的。然而,有许多不同的解决方案。这取决于您需要的严格程度,以及您是否有自定义的验证要求,或者是否接受任何有效的电子邮件地址。

参考页面:http://www.regular-expressions.info/email.html