tags:

views:

66

answers:

3

Hey.

Is there any python function that validates E-mail addresses, aware of IDN domains ? For instance, [email protected] should be as correct as user@zääz.de or user@納豆.ac.jp

Thanks.

+1  A: 

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.

kindall
A: 

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

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

And check to see if that opens up.

RobertPitt
This won't always work either. This method will look up the 'A' (address) record in DNS; however, this is often not the mail server. You need to get the 'MX' (mail exchange) record instead. The easiest way is, as @kindall said, to send an email to the address
lacqui
+1  A: 

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'[email protected]'); 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/

vls
Exactly what I was looking for. Thanks :)
Pierre