TNEF Parser for Node.js

1.4.0 · active · verified Tue Apr 21

node-tnef is a Node.js library designed to parse Transport Neutral Encapsulation Format (TNEF) files, commonly encountered as `winmail.dat` attachments in emails. This proprietary Microsoft format is often sent by Outlook users but is not natively understood by other email clients. The library extracts embedded content and attachments from these files, addressing a niche but persistent interoperability problem. It offers both a command-line interface for direct file or directory parsing and a programmatic API for use within Node.js projects, primarily through its `parse` (for file paths) and `parseBuffer` (for in-memory buffers) methods. The current stable version is 1.4.0. The package maintains an ad-hoc release cadence, focusing on bug fixes and feature enhancements, such as the addition of `parseBuffer` in version 1.3.0 and the removal of the `bluebird` dependency in 1.4.0.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `tnef.parseBuffer` to process an in-memory TNEF buffer and extract its contents, including writing an attachment to a file.

const tnef = require('node-tnef');
const fs = require('fs');
const path = require('path');

// A minimal, valid TNEF signature prefix for demonstration.
// A real winmail.dat file would be much larger and complex.
const dummyTnefBuffer = Buffer.from(
  '\x22\x34\x71\x9A\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
  'binary'
);

tnef.parseBuffer(dummyTnefBuffer, (err, content) => {
  if (err) {
    console.error('Error parsing TNEF buffer:', err);
    return;
  }

  if (content && content.length > 0) {
    console.log(`Found ${content.length} attachment(s).`);
    const firstAttachment = content[0];
    console.log('First attachment title:', firstAttachment.Title);

    // Example: Writing the attachment data to a file
    const outputPath = path.join(__dirname, firstAttachment.Title || 'extracted_attachment.bin');
    fs.writeFile(outputPath, firstAttachment.Data, (writeErr) => {
      if (writeErr) {
        console.error('Error writing attachment to file:', writeErr);
      } else {
        console.log(`Attachment written to: ${outputPath}`);
      }
    });
  } else {
    console.log('No attachments or content found in TNEF buffer.');
  }
});

view raw JSON →