
Milter.js
Make your mail server programmable with JavaScript.
Your mail server already knows how to receive and deliver email. Your application knows your users, your data, and your rules. Connecting the two should be simple.
Want to check a sender against your database before accepting a message? Ask an AI model whether an email is spam? Collect DMARC reports, inspect attachments, or mark messages for delivery to a particular folder? The tools are already available in Node.js. Milter.js brings them into your mail server's processing flow.
Milter.js lets you inspect and act on email as it arrives. Write ordinary JavaScript handlers for the sender, recipients, headers, or body. Call an API, query a database, or use an npm package, then decide what happens next.
server.on("bodyEnd", async (body, ctx) => {
const spam = await classifyMessage(body, ctx.headers);
if (spam) {
ctx.addHeader("X-Spam-Flag", "YES");
}
return "accept";
});
Here, classifyMessage is your own function. It could use a few rules, a local model, or an external service. Milter.js connects that logic to the mail server.
What You Can Build
- Custom spam filters: combine your own rules with OpenAI, Ollama, or another classifier.
- Sender checks: use SPF and DKIM results, domain intelligence, or your own allowlists and blocklists.
- Mail automation: collect reports and pass message data to your applications.
- Message routing: add headers that your delivery system uses to select a mailbox or folder.
- Message rewriting: update headers, change recipients or the envelope sender, and replace message bodies.
You control the policy. Milter.js handles the Milter protocol, connection state, and communication with the mail server.
How It Works
A milter is a program a mail server consults while processing an email. The mail server reports each requested stage of the SMTP transaction, and the filter returns a decision.
With Milter.js, those stages become JavaScript events:
| Event |
What you receive |
connect |
Information about the sending connection |
helo |
The sender's HELO or EHLO name |
mail |
The envelope sender |
rcpt |
An envelope recipient |
headers |
The message headers |
bodyEnd |
The collected message body |
Register the events you need. Handlers can be synchronous or asynchronous.
Milter.js runs alongside a Milter-compatible mail server such as Postfix or Sendmail. The mail server handles SMTP and delivery; your handlers provide the filtering logic. MIME parsing and spam classification can be added through the packages or services you choose.
Installation
Requires Node.js 22.19.0 or newer.
ES modules:
import { MilterServer, Decision } from "milter";
CommonJS:
const { MilterServer, Decision } = require("milter");
TypeScript declarations are included.
Your First Filter
Save this as filter.mjs:
import { MilterServer } from "milter";
const server = new MilterServer({
host: "127.0.0.1",
port: 7357
});
server.on("mail", (from) => {
console.log("Envelope sender:", from[0]);
return "continue";
});
server.on("headers", (headers) => {
console.log("Subject:", headers.subject ?? []);
return "continue";
});
server.on("bodyEnd", (_body, ctx) => {
ctx.addHeader("X-Processed-By", "Milter.js");
return "accept";
});
server.on("error", (error, ctx) => {
console.error("Milter error", ctx?.id, error);
});
await server.listen();
console.log("Milter listening on 127.0.0.1:7357");
Run it:
For Postfix, add these settings to main.cf:
smtpd_milters = inet:127.0.0.1:7357
non_smtpd_milters = inet:127.0.0.1:7357
milter_protocol = 6
milter_default_action = accept
Then reload Postfix:
The example adds an X-Processed-By header to messages that pass through the filter. milter_default_action = accept tells Postfix to keep accepting mail if the filter is unavailable; choose tempfail if mail should wait until filtering is available again.
Making Decisions
Return a decision from an event handler:
| Return value |
Meaning |
"continue" |
Continue filtering at the next stage. |
"accept" |
Accept and stop further filtering by this milter for the current message. |
"reject" |
Reject permanently. |
"tempfail" |
Defer delivery so the sending server can retry later. |
"discard" |
Accept the SMTP transaction, then silently discard the message. |
undefined |
Use defaultDecision, which is "continue" by default. |
Use "continue" when later handlers still need to inspect the message. For example, accepting at the sender stage ends this milter's filtering before its body handler runs.
The Decision helper provides equivalent reply objects and custom SMTP replies:
import { Decision } from "milter";
server.on("rcpt", (to) => {
const address = (to[0] ?? "").replace(/^<|>$/g, "");
if (address === "retired@example.com") {
return Decision.replyCode(
"550",
"This mailbox no longer accepts mail",
"5.1.1"
);
}
return Decision.continue();
});
Decision.accept(), reject(), tempfail(), and discard() are also available.
Working with Messages
Check the Sender
The mail event receives the decoded MAIL FROM fields as an array. The first field contains the envelope sender.
server.on("mail", (from) => {
const address = (from[0] ?? "")
.replace(/^<|>$/g, "")
.toLowerCase();
if (address.endsWith("@blocked.example")) {
return "reject";
}
return "continue";
});
The envelope sender is separate from the visible From header.
Read Headers
Header names are lowercase, and each value is an array because a header can appear more than once.
server.on("headers", (headers) => {
console.log("Subject:", headers.subject ?? []);
console.log("Content-Type:", headers["content-type"] ?? []);
});
Use headerLine(name, value, ctx) to process headers individually.
Inspect the Body
By default, Milter.js collects body chunks and passes the complete body to bodyEnd as a Buffer:
server.on("bodyEnd", (body, ctx) => {
console.log("Body bytes:", body.length);
console.log("Headers:", ctx.headers);
return "accept";
});
This is the raw message body, which can contain MIME parts and encoded content. Use a MIME parser when you need decoded text or attachments.
Modify a Message
Request the actions your filter needs, then make changes in bodyEnd:
import { MilterServer, SMFIF } from "milter";
const server = new MilterServer({
host: "127.0.0.1",
port: 7357,
actions:
SMFIF.ADDHDRS |
SMFIF.CHGHDRS |
SMFIF.ADDRCPT |
SMFIF.DELRCPT
});
server.on("bodyEnd", (_body, ctx) => {
ctx.addHeader("X-Filtered", "yes");
ctx.changeHeader("Subject", 1, "[Filtered] Message");
ctx.addRecipient("archive@example.com");
ctx.deleteRecipient("old-address@example.com");
return "accept";
});
await server.listen();