无法找到如何在App Engine中检查有效邮箱

3 投票
2 回答
1453 浏览
提问于 2025-04-16 08:46

有人知道哪里有相关的文档吗?

到目前为止,我只找到这个链接:

http://code.google.com/appengine/articles/djangoforms.html

EmailProperty() 只检查空字符串...

2 个回答

3

如果你查看谷歌的mail函数的源代码,你会发现mail.is_email_valid()这个函数其实只检查字符串是否为空或为None,也就是说它只看这个邮箱地址有没有内容。

在这个网站上,我找到了一种符合RFC822标准的Python邮箱地址验证器。

import re

qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
quoted_pair = '\\x5c[\\x00-\\x7f]'
domain_literal = "\\x5b(?:%s|%s)*\\x5d" % (dtext, quoted_pair)
quoted_string = "\\x22(?:%s|%s)*\\x22" % (qtext, quoted_pair)
domain_ref = atom
sub_domain = "(?:%s|%s)" % (domain_ref, domain_literal)
word = "(?:%s|%s)" % (atom, quoted_string)
domain = "%s(?:\\x2e%s)*" % (sub_domain, sub_domain)
local_part = "%s(?:\\x2e%s)*" % (word, word)
addr_spec = "%s\\x40%s" % (local_part, domain)


email_address = re.compile('\A%s\Z' % addr_spec)
# How this is used: 
def isValidEmailAddress(email):
    if email_address.match(email):
        return True
    else:
        return False

* 如果你打算使用这个验证器,请使用这个版本,因为它包含了创建者的名字等信息。

5

下面的代码是在服务器上验证电子邮件地址的:

from google.appengine.api import mail
if not mail.is_email_valid(to_addr):
  # Return an error message...

希望这对你有帮助?

撰写回答