Node.js is highly optimized for processing asynchronous I/O data streams, making it the perfect runtime for building custom inbound mail gateways. Let's write a simple script that captures raw SMTP mail from standard input and parses it into clean JSON.

1. Installing mailparser Library

We will use the highly reputed mailparser library to process the incoming MIME stream. Install it via npm: npm install mailparser.

2. Processing Standard Input (stdin)

Create a script named parser.js. The script reads the raw stdin stream, pipes it to the parser, and outputs structured JSON metadata:

const { simpleParser } = require("mailparser");

simpleParser(process.stdin)
  .then(mail => {
    console.log(JSON.stringify({
      from: mail.from.text,
      subject: mail.subject,
      text: mail.text,
      html: mail.html
    }, null, 2));
  })
  .catch(err => console.error("Parsing failed:", err));

This script can be integrated with Postfix virtual alias maps to process incoming emails programmatically at the edge, paving the way for custom mail integrations.