How to validate an EML Email File in Python

File validation is like a VIP pass that lets you skip past a whole array of different annoyances and bugs. This is certainly true for EML files, but this validation can be difficult to setup, as there are many ways in which a file can fail at being useful. Short of booting up Outlook to open the file, there isn’t really any clean way to get this done. That is, unless you have the API that I am demonstrating today.

Let’s start by installing our client with pip install, as you see with this command here:

pip install cloudmersive-convert-api-client

And now for the most code intensive part of this endeavor: our function call. Don’t worry, you can just copy and paste it.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_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_convert_api_client.ValidateDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Validate if an EML file is executableapi_response = api_instance.validate_document_eml_validation(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ValidateDocumentApi->validate_document_eml_validation: %s\n" % e)

Now provide an input file path and give it a test. That’s right, the entire process is already complete. Trivial!

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