How to Validate an Email Address for Correct Syntax in Python

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 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. \"\".    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.

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store