Dependency Check: In section # 2, the script now immediately checks for the existence of commands like awk, grep, tail, flock, etc., upon execution. If any are missing, the script exits safely with a clear error message instead of collapsing mid-run. Systemd (Journalctl) Support: The get_log_stream function was overhauled. If it finds no text logs in /var/log (which is standard on new systems without rsyslog), the script automatically calls journalctl SYSLOG_FACILITY=2. This works seamlessly in both standard mode and Live Watch (-w) mode. Automatic Pagination: The use_pager function was added at the end of the script. If the script detects it is running in a terminal, it pipes the entire output into less -R -F -X. This means if you output 1000 records, the text won't scroll off the screen; you can navigate with arrow keys. If there are few records (fitting on one screen), less automatically quits (-F). New Year's Eve Bug Fix: A CURRENT_MONTH variable was added to bash and passed into the AWK script (as cur_m). AWK then compares whether the month in the log is greater than the current month. If it is, the log is from the previous year, and for sorting purposes, it prefixes the sorting key with a 0, otherwise 1. This ensures December records appear before January records in the final sorted list.
Mail Stats - Postfix Summary Tool (v2026.1.1)
📋 Table of Contents
- Overview
- Features
- Requirements
- Installation
- Usage
- Options
- Examples
- Security & Hardening
- Notes
- License
- Author
🌟 Overview
Mail Stats is a secure, hardened, memory-safe Bash tool for summarizing Postfix mail logs. It provides:
- Filtering by sender, recipient, queue ID, and date
- Concise summary tables or detailed per-message views
- Automatic processing of compressed logs (
.gz) - Color-coded terminal output for easier readability
This tool is ideal for server administrators who need fast insights into sent, rejected, or deferred emails.
✨ Features
-
Color-coded output for quick status recognition
-
Filter by:
-
Sender (
from) -
Recipient (
to) -
Queue ID (
id) -
Date (
date)- Use
-d ""to search across all logs
- Use
-
-
Summary or detailed per-message view
-
Handles
.gzcompressed logs automatically -
Excludes local deliveries (
127.0.0.1) unless filtered -
Limits CPU, memory, and file usage for safety
🖥 Requirements
- Linux/Unix system
- Bash 4.x+
- Postfix logs in
/var/log/(e.g.,maillog*,mail.log*) awk,zcat,grep,find,sedinstalled
⚡ Installation
Copy or clone the script:
sudo cp mail-stats /usr/local/bin/mail-stats
sudo chmod +x /usr/local/bin/mail-stats
No additional dependencies required.
🛠 Usage
mail-stats [OPTIONS]
By default, it displays the last 10 mail log entries for today.
🔧 Options
| Option | Description |
|---|---|
-h, --help |
Show help message |
-l, --list NUMBER |
Number of records (default 10, max 1000) |
-f, --from EMAIL/DOMAIN |
Filter by sender |
-t, --to EMAIL/DOMAIN |
Filter by recipient |
-i, --id QUEUE_ID |
Show detailed view of specific queue ID |
-d, --date "MMM DD" |
Filter by date (e.g., "Mar 27") |
📝 Examples
Show last 20 emails:
mail-stats -l 20
Filter by sender:
mail-stats -f "example@domain.com"
Filter by recipient:
mail-stats -t "recipient@domain.com"
Detailed view for Queue ID:
mail-stats -i 3F2A4B1C0
Filter by date:
mail-stats -d "Mar 27"
Search all logs:
mail-stats -d ""
🛡 Security & Hardening
- Resource Limits
ulimit -t 15 # CPU time
ulimit -v 500000 # Virtual memory (kB)
ulimit -f 102400 # Max file size
- Single Instance Locking
- Prevents simultaneous executions via
/tmp/mail-stats.lock
- Temporary File Safety
- Uses
mktempand cleans files on exit
- Safe Argument Handling
- Limits input length and validates required arguments
- Memory-Safe Log Parsing
- Skips overly long/short lines
- Old Temp File Cleanup
- Deletes stale files older than 10 minutes
⚠ Notes
-
Ignores local Postfix deliveries (
127.0.0.1) by default -
Supports
.gzcompressed logs -
Detailed view (
--id) shows:- Date/Time
- Client IP
- Sender
- Recipient
- Status
- Reason
📜 License
MIT License – free to use, modify, and distribute
👤 Author
robott – GitHub

