How to Convert EML to HTML in Node.js

Cloudmersive
2 min readOct 16, 2023

--

Storing EML files in HTML format makes it possible to view them on the web without needing an email client.

Thankfully, with the ready-to-run Node.js code examples below, we can easily take advantage of a free API that converts EML files to HTML format. We can also specify if our conversion should include attachments or only the body of the email by customizing specific parameters in our request.

Before we customize our request, however, we’ll need to begin by installing the SDK. To do that, we can either run this command:

npm install cloudmersive-convert-api-client --save

Or we can alternatively add the below snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Now let’s include the below code in our file and customize our request parameters. If we set “bodyOnly” to True, our resulting HTML string will only include the body of the original email; other information, such as the subject, will be included separately as a property in our response object. If we set “includeAttachments” to False, our response object won’t include any attachment files from the original EML.

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // 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);

We can authorize our request with a free-tier Cloudmersive API key — this will allow us to make up to 800 API calls per month with no additional commitment.

Our response object will look something like this:

{
"Successful": true,
"Content": "string",
"Body": "string",
"From": "string",
"To": "string",
"Cc": "string",
"DateSent": "string",
"Subject": "string",
"Attachments": [
{
"Name": "string",
"Content": "string"
}
]
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet