STREAMING EMAIL SDK FOR NODE.JS
MailPort is a developer-focused Node.js 22+ email library. Engineered for zero-copy MIME generation, single-connection SMTP transport with mandatory TLS and SASL authentication, and zero-network in-memory test suites.
npm install mailport
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: 'App System <noreply@example.com>',
to: 'user@domain.com',
subject: 'Transactional Verification',
text: 'Your authentication code is 849204.',
html: '<h1>Verification</h1><p>Code: <strong>849204</strong></p>',
});
console.log('[MailPort Result]', result.status, result.accepted);
UNIFIED UMBRELLA PACKAGE
Recommended for standard applications. Includes core mailer engine, MIME stream builder, SMTP network transport, and in-memory testing utilities.
MODULAR PACKAGES
Install lightweight individual modules for strict bundle optimization and decoupling of transport dependencies.
MESSAGE COMPOSER
Execute simulation to generate MIME output.
Execute simulation to view inbox result.
Generating code snippet...
Unified entry package re-exporting core engine, MIME streams, SMTP transport, and testing matchers.
Mailer engine, address parsing, CR/LF injection safety checks, lifecycle hooks, and error hierarchy.
Zero-copy streaming MIME builder, Base64 chunk encoder, Quoted-Printable encoder, RFC 2231 filename parameters.
Single-connection SMTP transport with mandatory TLS upgrade, SASL PLAIN/LOGIN auth, and dot-stuffing.
Zero-network InMemoryTransport and inspectable InMemoryInbox for fast unit testing.
import { createMailer, smtp } from 'mailport';
const mailer = createMailer({
transport: smtp({
host: 'smtp.mailtrap.io',
port: 2525,
auth: {
user: process.env.SMTP_USER!,
pass: process.env.SMTP_PASS!,
},
}),
});
const result = await mailer.send({
from: 'Acme Security <security@acme.com>',
to: 'alice@example.com',
subject: 'Verification Code',
text: 'Code: 849204',
html: '<p>Code: <strong>849204</strong></p>',
});
console.log(result.id, result.accepted);
import { SmtpTransport } from '@mailport/smtp';
import { Mailer } from '@mailport/core';
const transport = new SmtpTransport({
host: 'smtp.sendgrid.net',
port: 587,
tls: 'starttls',
auth: { user: 'apikey', pass: process.env.SENDGRID_API_KEY! },
});
const mailer = new Mailer({ transport });
await transport.validate();
import { describe, expect, it } from 'vitest';
import { InMemoryTransport, createTestMailer } from '@mailport/testing';
describe('Registration Mailer', () => {
it('captures message in memory', async () => {
const transport = new InMemoryTransport();
const mailer = createTestMailer({ transport });
await mailer.send({
from: 'app@example.com',
to: 'user@domain.com',
subject: 'Welcome',
text: 'Hello',
});
expect(transport.inbox.count).toBe(1);
});
});
import { createReadStream } from 'node:fs';
import { createMailer, smtp } from 'mailport';
const mailer = createMailer({ transport: smtp({ host: 'smtp.example.com' }) });
await mailer.send({
from: 'billing@example.com',
to: 'customer@domain.com',
subject: 'Invoice',
text: 'Statement attached.',
attachments: [
{
type: 'path',
filename: 'statement.pdf',
path: './statement.pdf',
contentType: 'application/pdf',
},
],
});
import { Mailer, SmtpTransport } from 'mailport';
const mailer = new Mailer({
transport: new SmtpTransport({ host: 'smtp.example.com' }),
hooks: {
onBeforeSend(message, context) {
console.log(`[${context.attemptId}] Sending...`);
},
onAfterSend(result, message, context) {
console.log(`[${context.attemptId}] Delivered in ${result.durationMs}ms`);
},
},
});
ERROR CLASSIFICATION TABLE
| ERROR CLASS | CODE | RETRYABLE |
|---|---|---|
MailValidationError |
ERR_MAIL_VALIDATION |
NO |
MailAuthError |
ERR_MAIL_AUTH |
NO |
MailConnectionError |
ERR_MAIL_CONNECTION |
YES |
MailTlsError |
ERR_MAIL_TLS |
NO |
MailRateLimitError |
ERR_MAIL_RATE_LIMIT |
YES |
ADDRESS PARSING CONTRACT
import { parseAddress } from '@mailport/core';
// Display Name Format
parseAddress('Alice <alice@domain.com>');
// => { name: 'Alice', address: 'alice@domain.com' }
// Object Representation
parseAddress({ name: 'Bob', address: 'bob@domain.com' });
// Plain Address
parseAddress('user@domain.com');
Streams chunks directly to network sockets without memory buffering.
Instant header folding and boundary generation initialization.
Clean single-connection lifecycle eliminates stale pool corruption.