Sample code for 30+ languages & platforms
Node.js Requires Chilkat v11.0.0+

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat Node.js Downloads

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    var success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    var email = new chilkat.Email();
    success = email.LoadEml("with-attachments.eml");
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    var preview = new chilkat.Email();
    success = email.MakeCopy(preview);
    if (success !== true) {
        console.log(email.LastErrorText);
        return;
    }

    //  Keep related items: the HTML still refers to them.
    preview.DropAttachments();
    console.log("Files in original: " + email.NumAttachments + "  Files in preview: " + preview.NumAttachments);
    if (preview.HasHtmlBody() !== true) {
        console.log("This message has no HTML preview.");
        return;
    }

    var html = new chilkat.StringBuilder();
    success = preview.GetHtmlBodySb(true,html);
    if (success !== true) {
        console.log(preview.LastErrorText);
        return;
    }

    success = html.WriteFile("preview.html","utf-8",false);
    if (success !== true) {
        console.log("Failed: html.WriteFile.");
        return;
    }

    console.log("Saved preview.html.");

}

chilkatExample();