#!/bin/bash
REPORT_RECIPIENT="[email protected]"
MAIL_LOG="/var/log/mail.log"
REPORT_FILE="/tmp/daily_mail_report.txt"
YESTERDAY_MONTH_DAY=$(date --date="yesterday" +"%b %_d")
YESTERDAY_FULL=$(date --date="yesterday" +"%Y-%m-%d")
echo "Daily Postfix Mail Report for $YESTERDAY_FULL" > "$REPORT_FILE"
echo "---------------------------------------------------" >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "--- Mail Summary (Sent/Received) ---" >> "$REPORT_FILE"
grep "$YESTERDAY_MONTH_DAY" "$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"
echo "--- Detailed Mail Flow (Sender, Recipient, Subject - if available) ---" >> "$REPORT_FILE"
TEMP_LOG_LINES=$(grep "$YESTERDAY_MONTH_DAY" "$MAIL_LOG" | grep "postfix/")
echo "$TEMP_LOG_LINES" | awk '
/postfix/smtpd[/ {
# Using gensub for more robust extraction, or simpler string manipulation
ip_match = match($0, /client=[^ ]+\[([^\]]+)\]/);
if (ip_match) {
ip = substr($0, RSTART + 8, RLENGTH - 9); # Adjusting indices for client=[...]\[...]
} else {
ip = "";
}
user_match = match($0, /sasl_username=([^ ]+)/);
if (user_match) {
user = substr($0, RSTART + 14, RLENGTH - 14);
} else {
user = "";
}
}
/postfix/cleanup[.*]: message-id=</ {
msgid_match = match($0, /message-id=<([^>]+)>/);
if (msgid_match) {
msgid = substr($0, RSTART + 12, RLENGTH - 13);
} else {
msgid = "";
}
}
/postfix/qmgr[.*]: ([0-9A-F]+): from=</ {
qid = $6;
sender_match = match($0, /from=<([^>]+)>/);
if (sender_match) {
sender = substr($0, RSTART + 6, RLENGTH - 7);
} else {
sender = "";
}
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;
recipient_match = match($0, /to=<([^>]+)>/);
if (recipient_match) {
recipient = substr($0, RSTART + 4, RLENGTH - 5);
} else {
recipient = "";
}
status_match = match($0, /status=(sent|deferred|bounced)/);
if (status_match) {
status = substr($0, RSTART + 7, RLENGTH - 7);
} else {
status = "";
}
print " QID: " qid ", To: " recipient ", Status: " status;
}
' >> "$REPORT_FILE"
echo "" >> "$REPORT_FILE"
echo "--- Summary Counts ---" >> "$REPORT_FILE"
echo "Total messages processed (approx): $(grep "$YESTERDAY_MONTH_DAY" "$MAIL_LOG" | grep "postfix/qmgr" | grep "from=" | wc -l)" >> "$REPORT_FILE"
echo "Messages sent successfully: $(grep "$YESTERDAY_MONTH_DAY" "$MAIL_LOG" | grep "postfix/" | grep "status=sent" | wc -l)" >> "$REPORT_FILE"
echo "Messages deferred: $(grep "$YESTERDAY_MONTH_DAY" "$MAIL_LOG" | grep "postfix/" | grep "status=deferred" | wc -l)" >> "$REPORT_FILE"
echo "Messages bounced: $(grep "$YESTERDAY_MONTH_DAY" "$MAIL_LOG" | grep "postfix/" | grep "status=bounced" | wc -l)" >> "$REPORT_FILE"
echo "---------------------------------------------------" >> "$REPORT_FILE"
mail -s "Daily Postfix Mail Report - $YESTERDAY_FULL" "$REPORT_RECIPIENT" < "$REPORT_FILE"
rm "$REPORT_FILE"
echo "Daily mail report generated and sent to $REPORT_RECIPIENT."