Logging Outgoing Mail from Postfix with Node.js and TeqFW DI
Publication date: 2025-04-13While developing NutriLog, I needed to track all outgoing emails sent through Postfix. This led to the creation of a small tool — smtp-logger.
This is a simple Node.js utility that connects to Postfix as a pipe handler. It receives the complete email via `stdin`, extracts the sender, recipient, and subject, and saves the entire "raw" MIME structure to a SQL database (PostgreSQL, SQLite, MySQL, etc. via `knex`).
What makes this project interesting?
It's a working example of using ES6 modules and late dependency binding in JS architecture. The entire structure is built using @teqfw/di — a lightweight DI container compatible with native JavaScript (no TypeScript or bundlers required).
Features
- Integration with Postfix via `pipe` (e.g., through `always_bcc`)
- Extraction of `from`, `to`, and `subject` + saving the entire email
- Works with any database supported by `knex`
- CLI interface: `init-db` and `log` commands
- SQLite support for local logging
Important: the database schema is not created automatically. Before use, you must manually run the `init-db` command.
If you use Postfix only for outgoing mail — this is an excellent solution for auditing, debugging, and logging messages in production or staging environments.