CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mailparser

Advanced email parser for Node.js that handles email parsing as a stream for memory-efficient processing of large messages

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

simple-parser.mddocs/

Simple Email Parsing

High-level email parsing function that handles the complete parsing process and returns a structured mail object with all content and attachments. This is the recommended approach for most use cases.

Capabilities

simpleParser Function

Parses email data and returns a complete mail object with all parsed content, headers, and attachments.

/**
 * Parse email data into a structured mail object
 * @param input - Email data as string, Buffer, or readable stream
 * @param options - Optional parsing configuration
 * @param callback - Optional callback function (if not provided, returns Promise)
 * @returns Promise resolving to parsed mail object
 */
function simpleParser(
  input: string | Buffer | Stream,
  options?: ParseOptions,
  callback?: (err: Error | null, mail: ParsedMail) => void
): Promise<ParsedMail>;

Usage Examples:

const { simpleParser } = require('mailparser');
const fs = require('fs');

// Parse from file stream
const mail = await simpleParser(fs.createReadStream('message.eml'));
console.log(mail.subject);
console.log(mail.from.text);
console.log(mail.attachments.length);

// Parse from string with options
const emailString = `From: sender@example.com
To: recipient@example.com
Subject: Test Message

Hello World!`;

const mail = await simpleParser(emailString, {
  skipHtmlToText: true,
  keepCidLinks: true
});

// Parse with callback style
simpleParser(emailBuffer, (err, mail) => {
  if (err) {
    console.error('Parse error:', err);
    return;
  }
  console.log('Subject:', mail.subject);
  console.log('Text:', mail.text);
});

Parse Options

Configuration options for controlling parsing behavior.

interface ParseOptions {
  /** Keep CID links in HTML instead of converting to base64 data URLs */
  keepCidLinks?: boolean;
  /** Skip converting HTML content to plain text */
  skipHtmlToText?: boolean;
  /** Skip processing image src attributes */
  skipImageLinks?: boolean;
  /** Skip converting plain text to HTML */
  skipTextToHtml?: boolean;
  /** Skip auto-linking URLs in plain text */
  skipTextLinks?: boolean;
  /** Keep message/delivery-status parts as text instead of ignoring */
  keepDeliveryStatus?: boolean;
  /** Custom Iconv instance for character encoding */
  Iconv?: any;
  /** Algorithm for calculating attachment checksums (default: 'md5') */
  checksumAlgo?: string;
  /** Maximum HTML content length to parse (prevents memory issues) */
  maxHtmlLengthToParse?: number;
  /** Custom function for formatting date strings */
  formatDateString?: (date: Date) => string;
}

Parsed Mail Object

The complete structure returned by simpleParser containing all parsed email data.

interface ParsedMail {
  /** Map of all email headers (case-insensitive keys) */
  headers: Map<string, any>;
  /** Array of raw header lines as received */
  headerLines: HeaderLine[];
  /** HTML content of the email */
  html?: string;
  /** Plain text content of the email */
  text?: string;
  /** Plain text converted to HTML format */
  textAsHtml?: string;
  /** Email subject line */
  subject?: string;
  /** Parsed date from Date header */
  date?: Date;
  /** Parsed From addresses */
  from?: AddressObject;
  /** Parsed To addresses */
  to?: AddressObject;
  /** Parsed CC addresses */
  cc?: AddressObject;
  /** Parsed BCC addresses */
  bcc?: AddressObject;
  /** Message-ID header value */
  messageId?: string;
  /** In-Reply-To header value */
  inReplyTo?: string;
  /** Reply-To addresses */
  replyTo?: AddressObject;
  /** References header values as array */
  references?: string[];
  /** Array of all attachments found in the email */
  attachments: Attachment[];
}

Error Handling

// The function throws TypeError for invalid input
try {
  await simpleParser(null); // Throws TypeError
} catch (err) {
  console.error(err.message); // "Input cannot be null or undefined."
}

// Parse errors are returned/thrown normally
try {
  const mail = await simpleParser(corruptedEmailData);
} catch (err) {
  console.error('Failed to parse email:', err);
}

Input Types

The simpleParser function accepts three types of input:

  • String: Raw email content as text
  • Buffer: Email data as binary buffer
  • Stream: Readable stream containing email data
// String input
await simpleParser('From: test@example.com\n\nHello');

// Buffer input  
const buffer = Buffer.from(emailString, 'utf8');
await simpleParser(buffer);

// Stream input
const stream = fs.createReadStream('email.eml');
await simpleParser(stream);

docs

index.md

mail-parser.md

simple-parser.md

tile.json