How to convert an EML Email File to HTML String in Node.JS

Parsing an EML file into its base HTML is certainly a pain to set up from scratch. I would know, as I have done it. That’s why today I am going to pass this work on to you as an API, so that you don’t have to waste your time going through the same process. And so without further ado, let’s look at how this is going to work.

Start by running this command for pip install:

"dependencies": {
"cloudmersive-convert-api-client": "^2.1.6"

Then proceed with convertDocumentEmlToHtml, which will be called through an API instance. This is easy to set up as you will see below here.

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var opts = {'bodyOnly': true, // Boolean | 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.'includeAttachments': true // Boolean | Optional; If false, the response object will not include any attachment files from the input file. Default is true.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertDocumentEmlToHtml(inputFile, opts, callback);

Now we can optionally choose if we only want to include the email’s body in our main result (excluding headers such as To and From — these will still be included elsewhere in the response object), and whether we want the email’s attachments as byte arrays. And that’s about it. Call the function and you will get your resulting HTML string!

Image for post
Image for post

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