22 May 2013

To test if a given string is indeed a valid email address is an unsolved problem in programming. The internet is full of endless threads of which regular expression would let all valid addresses pass and forbid all invalid ones. The only agreement is that there is no agreement.

So I welcomed the inspiration of my colleague Jim (at Vokomokum) to implement something straightforward: Check everything before @ to be a valid name (consisting of letters, digits and a couple other characters). Then use some library to check if everything after @ is a mail host currently known on the internet.

I was sold. That seems like a really nice trade-off between quick implementation and effectiveness. It is mostly the host name that is hard to check, so let's simply see if it exists. Mail hosts don't disappear and reappear, so it's pretty safe to ask for the DNS system to know about them at any time. The only drawback here is that you need an internet connection to run this test.

Anyway, here is an example implementation of this in Python:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    def validate_email(address):
        ''' check email '''
        # check general form: a valid local name + @ + some host
        if not re.match('[A-Za-z0-9\-\_\.\+\$\%\#\&\*\/\=\?\{\}\|\~]+@[^@]+',
                        address): 
            raise('The email address does not '\
                  'seem to be valid.')
        # check host
        host = re.findall('[^@]+', address)[1]
        try:
            # dns.resolver throws an exception when it can't find a mail (MX)
            # host. The point at the end makes it not try to append domains
            _ = dns.resolver.query('{}.'.format(host), 'MX')
        except:
            raise('The host {} is not known in the DNS'\
                  ' system as a mail server.'\
                  ' Is it spelled correctly?'.format(host))

The dns.resolve module has to be installed, as it is not in the Standard Library. It is in the pythondns package, so you could do

pip install pythondns

 

# lastedited 09 Jul 2013
You are seeing a selection of all entries on this page. See all there are.