How to convert an EML Email File to HTML String in Python

Cloudmersive
2 min readJun 13, 2020

For those situations where HTML is more convenient than a bunch of clunky EML files, this solution is for you. We are going to tackle this problem with the greatest of ease, thanks in no small part to our guest star: a lovely Cloudmersive API. Let’s begin.

Package installation is our first step. Let’s use pip install for that.

pip install cloudmersive-convert-api-client

And now for our function call, which you can customize using its parameters.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest 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.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.body_only = true # bool | Optional; If true, the HTML string will only include the body of the email. Other information such as subject will still be given as properties in the response object. Default is false. (optional)include_attachments = true # bool | Optional; If false, the response object will not include any attachment files from the input file. Default is true. (optional)try:# Convert Email EML file to HTML stringapi_response = api_instance.convert_document_eml_to_html(input_file, body_only=body_only, include_attachments=include_attachments)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_eml_to_html: %s\n" % e)

And with that, our setup is complete. Now run it and you’re done!

--

--

Cloudmersive

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