MailPort is a developer-friendly Node.js email gateway built for performance. Streaming MIME generation, robust SMTP delivery, zero third-party core dependencies, and built-in testing utilities.
import { createMailer, smtp } from 'mailport';
const mailer = createMailer({
transport: smtp({
host: 'smtp.example.com',
port: 587,
tls: 'starttls',
auth: {
user: process.env.SMTP_USER!,
pass: process.env.SMTP_PASS!,
},
}),
});
const result = await mailer.send({
from: 'hello@example.com',
to: 'user@example.com',
subject: 'Hello from MailPort ๐',
text: 'MailPort is fast, type-safe, and streaming.',
});
console.log('Status:', result.status);
Engineered from the ground up for safety, speed, and clean ergonomics.
Strict type definitions for message options, envelope validation, transport contracts, and typed error hierarchies.
Emits RFC-compliant MIME streams directly from AsyncIterable sources with constant ~10 MB heap memory usage on 100 MB attachments.
Strict TLS certificate and hostname validation enabled by default with support for required STARTTLS and implicit TLS.
Includes `@mailport/testing` with `InMemoryTransport` and inspectable `InMemoryInbox` to test email sending without network mocks.
`@mailport/core` has exactly zero third-party runtime dependencies, minimizing supply chain vulnerabilities.
Accurately classifies delivery failures into `not-sent`, `uncertain`, and `sent` states to avoid duplicate message sends.
Simulate constructing a message and streaming dot-stuffed MIME payloads live in your browser.
You can install the unified umbrella package or individual modular packages depending on your project needs.
npm install mailport
npm install @mailport/core @mailport/smtp @mailport/testing
Construct a Mailer instance using `smtp()` transport and send a message:
import { createMailer, smtp } from 'mailport';
const mailer = createMailer({
transport: smtp({
host: 'smtp.example.com',
port: 587,
tls: 'starttls',
auth: {
user: process.env.SMTP_USER!,
pass: process.env.SMTP_PASS!,
},
}),
});
const result = await mailer.send({
from: 'noreply@yourdomain.com',
to: 'user@example.com',
subject: 'Order Confirmation #10293',
text: 'Thank you for your order!',
html: '<h1>Thank you for your order!</h1><p>Your items are being prepared.</p>',
});
console.log('Result ID:', result.id);
console.log('Accepted recipients:', result.accepted);
`@mailport/smtp` supports STARTTLS, implicit TLS, and SASL PLAIN / LOGIN authentication over secure TLS sockets.
import { smtp } from '@mailport/smtp';
const transport = smtp({
host: 'smtp.provider.com',
port: 465,
tls: 'implicit', // 'implicit' | 'starttls' | 'none'
auth: {
user: 'user@provider.com',
pass: 'secret-password',
method: 'plain', // 'plain' | 'login'
},
timeoutMs: 30000,
});
Use `InMemoryTransport` in Vitest or Jest to inspect sent emails without real network sockets:
import { describe, expect, it } from 'vitest';
import { InMemoryTransport, createTestMailer } from '@mailport/testing';
describe('User Registration Mailer', () => {
it('captures welcome email in memory', async () => {
const transport = new InMemoryTransport();
const mailer = createTestMailer({ transport });
await mailer.send({
from: 'app@example.com',
to: 'newuser@example.com',
subject: 'Welcome to App',
text: 'Thanks for signing up!',
});
expect(transport.inbox.count).toBe(1);
const latest = transport.inbox.latest();
expect(latest?.to[0].address).toBe('newuser@example.com');
});
});
MailPort classifies all operational and delivery errors into clear, actionable types:
MailError: Base error class with code, category, and retryable flag.
MailValidationError: Invalid message options, invalid address format, or missing required fields.
MailAuthError: Invalid SMTP username/password credentials (435 / 535 codes).
MailConnectionError: Socket connect timeout, network drop, or reset (classified as `deliveryState:
'not-sent'`).
MailUncertainError: Network failure after body transmission terminator (`deliveryState:
'uncertain'`).
MailCancelledError: Operation aborted via `AbortSignal` (`deliveryState: 'not-sent'`).
createMailer(options)Creates a type-safe Mailer instance wrapping a transport implementation.
smtp(options: SmtpTransportOptions)Factory function creating an SmtpTransport instance.
InMemoryTransport(options?: InMemoryTransportOptions)Test transport storing messages in memory.