Email Validation with domain

livibetter livibetter at gmail.com
Wed Jul 2 14:52:07 CEST 2008


> If you want to validate the domain, do a DNS lookup on the domain or
> some such. I don't think there are standard modules to provide this
> functionality included with python. You could try using the socket
> module, and reading up on the relevant protocols, or making calls to
> external programs.

I agreed. I made quick code for this.

# Email address validator
#
# This module is in Public Domain
#
# This module was written for replying to
# http://groups.google.com/group/comp.lang.python/browse_thread/thread/80d7d31bebc09190
#  * It requires dnspython (http://www.dnspython.org/).
#  * It is a simple prototype.
#  * It would be slow if query mass email addresses, having cache
mechanism
#    would be very helpful.
#  * It only checks hostname of email address.
#
# Author       : Yu-Jie Lin
# Creation Date: 2008-07-02T20:09:07+0800


import dns.resolver


def CheckEmail(email):
    """This function directly extracts the hostname and query it"""
    email_parts = email.split('@')
    if len(email_parts) != 2:
        return False

    # Start querying
    try:
        answers = dns.resolver.query(email_parts[1], 'MX')
    except dns.resolver.NoAnswer:
        # This host doesn't have MX records
        return False
    except dns.resolver.NXDOMAIN:
        # No such hostname
        return False

    # Possible a valid hostname
    return True

I also wrote a short blog post for this post and the code, if you are
interested, you can read it at http://thetinybit.com/Blog/2008-07-02-2047-EmailHostnameCheck



More information about the Python-list mailing list