开发者

Function to validate an E-mail (IDN aware)

Is there any python function that validates E-mail addresses, aware of IDN domains ? For instance, user@example开发者_如何学C.com should be as correct as user@zääz.de or user@納豆.ac.jp

Thanks.


Django supports IDN email validation as of version 1.2.

See the code for validation here: http://code.djangoproject.com/svn/django/trunk/django/core/validators.py

Reference: http://docs.djangoproject.com/en/1.2/ref/forms/fields/#emailfield

Example:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.core.validators import validate_email
>>> try: validate_email(u'user@example.com'); print "passed"
... except: print "failed"
... 
passed
>>> try: validate_email(u'user@zääz.de'); print "passed"
... except: print "failed"
... 
passed
>>> try: validate_email(u'user@納豆.ac.jp'); print "passed"
... except: print "failed"
... 
passed
>>> try: validate_email(u'this-should-fail@@納豆.ac.jp'); print "passed"
... except: print "failed"
... 
failed

You may need to define some environment settings before you can use Django modules, see documentation here: http://docs.djangoproject.com/en/dev/


It is very difficult to validate an e-mail address because the syntax is so flexible. The best strategy is to send a test e-mail to the entered address.


I think the best method is to open the mail server port via a HTTP Request and see if it responds

web = httplib.HTTPConnection(domain, 25, timeout=5)
web.connect()

And check to see if that opens up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜