letterparser

Raw e-mail parsing with MIME and plaintext support (isomorphic)

Usage no npm install needed!

<script type="module">
  import letterparser from 'https://cdn.skypack.dev/letterparser';
</script>

README

letterparser

workflow npm npm NPM

letterparser is a parser library created for parsing e-mail messages. The library is written in TypeScript, fully supports both browser and server environments. The performance may not be the best at the current stage of development, parsing large messages is not recommended.

This library was created as an isomorphic alternative for mailparser.

The following RFCs are supported (or will be) by letterparser:

Parsing multipart and plain text messages is currently working, although the output is raw. A function for extracting the most commonly used data will be added in a future release.

Builder Inbound SMTP
letterbuilder microMTA

Usage

WARNING! node.js built with full ICU is required. (full-icu NPM package may work as a substitute, although this is not recommended.)

By default, recent node.js versions ship full ICU binaries. Incomplete ICU will result in bad encoding errors.

General information

To get information about the message, use extract:

import { extract } from 'letterparser';
let res = extract(`Date: Wed, 01 Apr 2020 00:00:00 -0000
From: A <a@example.com>
To: B <b@example.com>
Subject: Hello world!
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8

Some message.`);

The function returns LetterparserMail:

export interface LetterparserAttachment {
  contentType: LetterparserContentType;
  body: string | Uint8Array;
}

export interface LetterparserMail {
  subject?: string;
  to?: string[];
  cc?: string[];
  bcc?: string[];
  from?: string;
  attachments?: LetterparserAttachment[];
  html?: string;
  text?: string;
  amp?: string;
}

Message structure

The library also exports a parse function that outputs the raw structure of the message.

import { parse } from 'letterparser';
let node = parse(`Date: Wed, 01 Apr 2020 00:00:00 -0000
From: A <a@example.com>
To: B <b@example.com>
Subject: Hello world!
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8

Some message.`);

The return value of that function is LetterparserNode, as defined below:

interface LetterparserContentType {
  type: string;
  encoding?: string;
  parameters: Headers;
}

interface LetterparserNode {
  contentType: LetterparserContentType;
  headers: Headers;
  body: LetterparserNode | LetterparserNode[] | string | Uint8Array;
}