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.
To check whether the email address firstname.lastname@example.org is valid, we can just call the `validate_email` function with that email as the argument. A full implementation would look like this:
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:
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.
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:
For the above request, we get a result of the following format: