How to Validate an Email Address for Correct Syntax in Python

Cloudmersive
2 min readJun 2, 2020

--

Email address validation is essential for eliminating potential frustration and wasted resources caused by fake addresses. Today we will be using an API to set up a syntax-based validation system, which will simply check the basic structure of an email address. For a more sophisticated system that checks the domain and queries the mail server, you can use the full email validation function from the same library we are using today.

Let’s install our API client as our initial step, using this command here:

pip install cloudmersive-validate-api-client

Now if we make an API instance via an API key, we can use it to call email_post, which will accept our email address as an argument.

from __future__ import print_functionimport timeimport cloudmersive_validate_api_clientfrom cloudmersive_validate_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_validate_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_validate_api_client.EmailApi(cloudmersive_validate_api_client.ApiClient(configuration))value = 'value_example' # str | Email address to validate, e.g. \"support@cloudmersive.com\".    The input is a string so be sure to enclose it in double-quotes.try:# Validate email adddress for syntactic correctness onlyapi_response = api_instance.email_post(value)pprint(api_response)except ApiException as e:print("Exception when calling EmailApi->email_post: %s\n" % e)

And there you have it: email validation in a nutshell.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet