Text Share Online

d

#!/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 YYYY-MM-DD format, which matches your log entries
YESTERDAY_FULL=$(date –date=”yesterday” +”%Y-%m-%d”)

# — Start of Report Generation —
echo “Daily Postfix Mail Report for $YESTERDAY_FULL” > “$REPORT_FILE”
echo “—————————————————” >> “$REPORT_FILE”
echo “” >> “$REPORT_FILE”

# Option 1: Simple summary of sent/received
echo “— Mail Summary (Sent/Received) —” >> “$REPORT_FILE”
# Now grepping for the YYYY-MM-DD format
grep “$YESTERDAY_FULL” “$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
# Now grepping for the YYYY-MM-DD format
TEMP_LOG_LINES=$(grep “$YESTERSDAY_FULL” “$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”
# Now grepping for the YYYY-MM-DD format
echo “Total messages processed (approx): $(grep “$YESTERDAY_FULL” “$MAIL_LOG” | grep “postfix/qmgr” | grep “from=” | wc -l)” >> “$REPORT_FILE”
echo “Messages sent successfully: $(grep “$YESTERDAY_FULL” “$MAIL_LOG” | grep “postfix/” | grep “status=sent” | wc -l)” >> “$REPORT_FILE”
echo “Messages deferred: $(grep “$YESTERDAY_FULL” “$MAIL_LOG” | grep “postfix/” | grep “status=deferred” | wc -l)” >> “$REPORT_FILE”
echo “Messages bounced: $(grep “$YESTERDAY_FULL” “$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 – $YESTERDAY_FULL” “$REPORT_RECIPIENT” < “$REPORT_FILE”

# Clean up the temporary report file
rm “$REPORT_FILE”

echo “Daily mail report generated and sent to $REPORT_RECIPIENT.”

Share This: