#!/bin/bash
# Define recipient email address
REPORT_RECIPIENT=”[email protected]” # <— IMPORTANT: Change this to your email address!
# Define the log file (adjust if your mail logs are in a different location)
MAIL_LOG=”/var/log/mail.log”
# Define the report file
REPORT_FILE=”/tmp/daily_mail_report.txt”
# Get yesterday’s date in a format that matches the log entries (e.g., “Jan 01”)
# For logs that include the year, you might need to adjust this.
# For logs that only show month and day, this works well.
YESTERDAY=$(date -d “yesterday” +”%b %e”)
# For logs that include the year, you might use:
# YESTERDAY_FULL=$(date -d “yesterday” +”%Y-%m-%d”) # if logs use YYYY-MM-DD
# YESTERDAY_MONTH_DAY=$(date -d “yesterday” +”%b %e”) # if logs use Mon Day
# — Start of Report Generation —
echo “Daily Postfix Mail Report for $(date -d “yesterday” +”%Y-%m-%d”)” > “$REPORT_FILE”
echo “—————————————————” >> “$REPORT_FILE”
echo “” >> “$REPORT_FILE”
# Option 1: Simple summary of sent/received
echo “— Mail Summary (Sent/Received) —” >> “$REPORT_FILE”
grep “$YESTERDAY” “$MAIL_LOG” | grep “postfix/” | grep -E “status=(sent|deferred|bounced)” | awk ‘{print $1, $2, $3, $6, $7, $8, $9, $10, $11, $12, $13, $14}’ >> “$REPORT_FILE”
echo “” >> “$REPORT_FILE”
# Option 2: More detailed view (example – adjust as needed)
echo “— Detailed Mail Flow (Sender, Recipient, Subject – if available) —” >> “$REPORT_FILE”
# This is a more complex parsing. It tries to link queue IDs to sender/recipient.
# Note: Extracting subject directly from logs is often not straightforward as it’s not always logged.
# We’ll focus on sender and recipient.
# Temporarily store relevant log lines for yesterday
TEMP_LOG_LINES=$(grep “$YESTERDAY” “$MAIL_LOG” | grep “postfix/”)
echo “$TEMP_LOG_LINES” | awk ‘
/postfix/smtpd[/ {
match($0, /client=[^ ]+[([^]]+)]/);
ip = substr($0, RSTART + 7, RLENGTH – 8);
match($0, /sasl_username=([^ ]+)/);
user = substr($0, RSTART + 14, RLENGTH – 14);
}
/postfix/cleanup[.*]: message-id=</ {
match($0, /message-id=<([^>]+)>/);
msgid = substr($0, RSTART + 12, RLENGTH – 13);
}
/postfix/qmgr[.*]: ([0-9A-F]+): from=</ {
qid = $6;
match($0, /from=<([^>]+)>/);
sender = substr($0, RSTART + 6, RLENGTH – 7);
if (msgid != “”) {
print “Message ID: ” msgid “, QID: ” qid “, From: ” sender “, Client IP: ” ip “, User: ” user;
} else {
print “QID: ” qid “, From: ” sender “, Client IP: ” ip “, User: ” user;
}
msgid = “”; # Reset for next message
ip = “”; user = “”;
}
/postfix/(smtp|local|virtual)[.*]: ([0-9A-F]+): to=</ {
qid = $6;
match($0, /to=<([^>]+)>/);
recipient = substr($0, RSTART + 4, RLENGTH – 5);
match($0, /status=(sent|deferred|bounced)/);
status = substr($0, RSTART + 7, RLENGTH – 7);
print ” QID: ” qid “, To: ” recipient “, Status: ” status;
}
‘ >> “$REPORT_FILE”
echo “” >> “$REPORT_FILE”
echo “— Summary Counts —” >> “$REPORT_FILE”
echo “Total messages processed (approx): $(grep “$YESTERDAY” “$MAIL_LOG” | grep “postfix/qmgr” | grep “from=” | wc -l)” >> “$REPORT_FILE”
echo “Messages sent successfully: $(grep “$YESTERDAY” “$MAIL_LOG” | grep “postfix/” | grep “status=sent” | wc -l)” >> “$REPORT_FILE”
echo “Messages deferred: $(grep “$YESTERDAY” “$MAIL_LOG” | grep “postfix/” | grep “status=deferred” | wc -l)” >> “$REPORT_FILE”
echo “Messages bounced: $(grep “$YESTERDAY” “$MAIL_LOG” | grep “postfix/” | grep “status=bounced” | wc -l)” >> “$REPORT_FILE”
echo “—————————————————” >> “$REPORT_FILE”
# Send the report via email
mail -s “Daily Postfix Mail Report – $(date -d “yesterday” +”%Y-%m-%d”)” “$REPORT_RECIPIENT” < “$REPORT_FILE”
# Clean up the temporary report file
rm “$REPORT_FILE”
echo “Daily mail report generated and sent to $REPORT_RECIPIENT.”