📨 A lightweight Node.js utility for logging received emails to a database.
Designed to be used as a pipe-based handler in Postfix (e.g. via always_bcc),
it parses and stores full email messages in a SQL database using knex.
Dependency injection is powered by @teqfw/di.
- Works as a
stdin-based mail handler (e.g. from Postfix aliases) - Parses full MIME email (headers + plain text or HTML body)
- Logs to PostgreSQL, SQLite, MySQL, or any other Knex-supported DB
- Modular architecture with dependency injection
- Supports both SQLite file-based and network database connections
- Automatic database schema initialization
- Audit/logging of all outgoing emails in multi-app environments
- Integrates into Postfix using
always_bccor direct aliases - Useful in production and staging for tracking system-generated messages
- Node.js 18+
knex(tested with PostgreSQL, SQLite, and MySQL)@teqfw/dimailparserminimistdotenv
git clone https://github.com/yourname/smtp-logger.git
cd smtp-logger
npm installDB_CLIENT=better-sqlite3
DB_FILE=./var/data.sqlite3DB_CLIENT=pg # or mysql, etc.
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
DB_USER=user
DB_PASS=passwordnode index.js log --dbClient=better-sqlite3 --dbFile=./var/data.sqlite3node index.js init-dbPipe an email to the utility:
cat sample.eml | node index.jsOr test manually:
cat sample.eml | node index.js logEdit /etc/aliases:
logger: "|/usr/local/bin/smtp-logger/index.js"Then update aliases:
sudo newaliasesIn /etc/postfix/main.cf:
always_bcc = logger@localhostUse this script (/tmp/log.sh) to redirect output to a file:
#!/bin/sh
cd /path/to/smtp-logger/
./index.js >> /tmp/smtp-logger.log 2>&1Edit /etc/aliases:
logger: "|/tmp/log.sh"Then update aliases:
sudo newaliasesApache License 2.0
© Alex Gusev & LLMs, 2025