Skip to main content

PostalMime

The main class for parsing RFC822 email messages.

Import

import PostalMime from 'postal-mime';

Static Methods

PostalMime.parse()

Parse an email message using the static method (recommended for most use cases).

PostalMime.parse(email, options?) -> Promise<Email>

Parameters

ParameterTypeRequiredDescription
emailRawEmailYesThe raw email content to parse
optionsPostalMimeOptionsNoParsing configuration options

RawEmail Types

The email parameter accepts multiple input formats:

TypeDescription
stringRaw email as a string
ArrayBufferRaw email as ArrayBuffer
Uint8ArrayRaw email as Uint8Array
BlobBlob object (browser only)
BufferNode.js Buffer
ReadableStreamWeb ReadableStream

Options

OptionTypeDefaultDescription
rfc822AttachmentsbooleanfalseTreat message/rfc822 parts without Content-Disposition as attachments
forceRfc822AttachmentsbooleanfalseTreat all message/rfc822 parts as attachments
attachmentEncodingstring'arraybuffer'How to encode attachment content: 'arraybuffer', 'base64', or 'utf8'
maxNestingDepthnumber256Maximum MIME part nesting depth
maxHeadersSizenumber2097152Maximum header size in bytes (2MB)

Returns

Promise<Email> - A Promise that resolves to the parsed email object.

Example

import PostalMime from 'postal-mime';

const rawEmail = `From: sender@example.com
To: recipient@example.com
Subject: Hello
Content-Type: text/plain

Hello, World!`;

const email = await PostalMime.parse(rawEmail);
console.log(email.subject); // "Hello"

With Options

const email = await PostalMime.parse(rawEmail, {
attachmentEncoding: 'base64',
maxNestingDepth: 50
});

Constructor

new PostalMime()

Create a new parser instance.

new PostalMime(options?)

Parameters

ParameterTypeRequiredDescription
optionsPostalMimeOptionsNoParsing configuration options

Example

const parser = new PostalMime({ attachmentEncoding: 'base64' });
const email = await parser.parse(rawEmail);
caution

Parser instances cannot be reused. Create a new instance for each email you parse.

Instance Methods

parse()

Parse an email message.

parser.parse(email) -> Promise<Email>

Parameters

ParameterTypeRequiredDescription
emailRawEmailYesThe raw email content to parse

Returns

Promise<Email> - A Promise that resolves to the parsed email object.

Example

const parser = new PostalMime();
const email = await parser.parse(rawEmail);

Email Object

The parsed email object contains the following properties:

Headers

PropertyTypeDescription
headersHeader[]Array of all headers
email.headers.forEach(header => {
console.log(header.key); // Lowercase header name
console.log(header.value); // Header value
});

Addresses

PropertyTypeDescription
fromAddress | undefinedFrom address
senderAddress | undefinedSender address
replyToAddress[] | undefinedReply-To addresses
toAddress[] | undefinedTo addresses
ccAddress[] | undefinedCC addresses
bccAddress[] | undefinedBCC addresses
deliveredTostring | undefinedDelivered-To address (string only)
returnPathstring | undefinedReturn-Path address (string only)

Message Identifiers

PropertyTypeDescription
messageIdstring | undefinedMessage-ID header
inReplyTostring | undefinedIn-Reply-To header
referencesstring | undefinedReferences header

Content

PropertyTypeDescription
subjectstring | undefinedSubject line (decoded)
datestring | undefinedDate in ISO 8601 format
textstring | undefinedPlain text content
htmlstring | undefinedHTML content
attachmentsAttachment[]Array of attachments

Complete Example

import PostalMime from 'postal-mime';

async function processEmail(rawEmail) {
const email = await PostalMime.parse(rawEmail, {
attachmentEncoding: 'base64'
});

console.log('From:', email.from?.address);
console.log('To:', email.to?.map(a => a.address).join(', '));
console.log('Subject:', email.subject);
console.log('Date:', email.date);

if (email.text) {
console.log('Text content:', email.text);
}

if (email.html) {
console.log('HTML content available');
}

console.log(`Attachments: ${email.attachments.length}`);
email.attachments.forEach(att => {
console.log(` - ${att.filename} (${att.mimeType})`);
});
}

TypeScript

import PostalMime from 'postal-mime';
import type { Email, PostalMimeOptions } from 'postal-mime';

const options: PostalMimeOptions = {
attachmentEncoding: 'base64',
maxNestingDepth: 100
};

const email: Email = await PostalMime.parse(rawEmail, options);

Error Handling

try {
const email = await PostalMime.parse(rawEmail);
} catch (error) {
if (error.message.includes('nesting depth')) {
console.error('Email has too many nested parts');
} else if (error.message.includes('header size')) {
console.error('Email headers are too large');
} else if (error.message.includes('Can not reuse parser')) {
console.error('Parser instance already used');
} else {
console.error('Parsing failed:', error);
}
}

See Also