How to validate an email address in Python

Last Updated Jan 26, 2021
Emma Jagger

Engineer, maker, Google alumna, CMU grad

Email address validation is an important part of maintaining a clean email list. Verifying email addresses improves your delivery rate and protects your sender reputation. To keep your email address clean, it’s important to validate email addresses when they are first provided and to validate your entire database periodicially. By validating emails when they are first provided, you can catch emails entered incorrectly through human error.

This post covers how to validate an email address in Python.

Validating an email address using an open-source Python library

To validate an email address in Python, you can use the email-validator library. This open-source library confirms that an email address has the correct syntax and that the domain name resolves.

To check whether the email address test@abstractapi.com is valid, we can just call the `validate_email` function with that email as the argument. A full implementation would look like this:


from email_validator import (
    validate_email,
    EmailNotValidError
)

try:
    # Check whether email is valid. If it is not, will throw an exception
    valid_result = validate_email(‘test@abstractapi.com’)
except EmailNotValidError as e:
   # Email not valid. This will be an EmailSyntaxError if the form of the address is invalid or an EmailUndeliverableError if the domain does not resolve.
    print(str(e))

If the email address is invalid, `validate_email` throws an `EmailNotValidError`. In this casse, however, the returned `valid_result` object will be of the form:


valid_result(
    email='test@abstractapi.com',
    local_part='test',
    domain=’abstractapi.com',
    ascii_email='test@abstractapi.com',
    ascii_local_part='test',
    ascii_domain='abstractapi.com',
    smtputf8=False
)

Drawbacks to using Python's open-source libraries for email validation

While this library provides a simple interface for validating emails, the information returned is fairly limited. This library only checks if the email syntax is correct and if the domain resolves without providing other descriptive information. Additionally, because invalid emails are handled as exceptions, invalid emails require a separate workflow from valid emails.

Validating emails in Python using Abstract's email validation API

There are a number of API's for email validation, although Abstract offers a free email validation API that offers a variety of additional information on validated email addresses. It also includes detailed documentation that covers how to use the API and the variety of information that it can provide.

Compared to the previous method of email validation, Abstract’s easy-to-use API provides more descriptive information about the email address being validated. For example, this API identifies whether emails are free or disposable and returns a quality score for all valid emails. This is similar to how IP geolocation API's can check if an IP address is using a VPN or proxy. This information can help you to more effectively analyze the emails contained within your database.

With the email validation API, we can easily check whether test@abstract_api.com is a valid email:


import requests

# Your API key, available from your account page
YOUR_EMAIL_VALIDATION_KEY = 'your_key'

test_email = 'test@abstract_api.com'
# Build the request URL containing the API key from your user settings and the email you'd like to test
request_url = 'https://emailvalidation.abstractapi.com/v1/?api_key=' + YOUR_EMAIL_VALIDATION_KEY + '&email=' + test_email
response = requests.get(request_url)

# To easily parse the response, we can convert the data to a dictionary:
result = json.loads(response.content)

For the above request, we get a result of the following format:


{
    'email': 'test@abstractapi.com',
    'autocorrect': '',
    'deliverability': 'DELIVERABLE',
    'quality_score': '0.80',
    'is_valid_format': {
        'value': True,
        'text': 'TRUE'
    },
    'is_free_email': {
        'value': False,
        'text': 'FALSE'
    },
    'is_disposable_email': {
        'value': False,
        'text': 'FALSE'
    },
    'is_role_email': {
        'value': True,
        'text': 'TRUE'
    },
    'is_catchall_email': {
        'value': None,
        'text': 'UNKNOWN'
    },
    'is_mx_found': {
        'value': True,
        'text': 'TRUE'
    },
    'is_smtp_valid': {
        'value': None,
        'text': 'UNKNOWN'
    }
}

Abstract makes email validation with Python simple.
Get started