new-site/scripts/bounce-watcher.sh
justin 71d466c922 Wire createCommission() into compliance batch checkout
Compliance batch orders now create commission ledger entries when
a discount code (agent referral) is used. Tracks total order amount,
discount applied, and links to the agent for payout processing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-11 11:00:38 -05:00

71 lines
2.7 KiB
Bash

#!/bin/bash
# Postfix bounce watcher — tails mail.log and reports bounces to Listmonk.
# Only processes bounces for campaign emails (from=<noreply@performancewest.net>).
# Ignores system cron bounces, postmaster notices, etc.
#
# Install: copy to /usr/local/bin/postfix-bounce-notify.sh
# Service: systemd unit at /etc/systemd/system/pw-bounce-watcher.service
LOG=/var/log/mail.log
AUTH="api:6X1rKPea61N4rZ1S65Hx5zvqzbCj30F6nvEe9oVGH_Y"
API="http://localhost:9100"
# Track queue IDs from campaign sends (from=noreply@) so we only
# report bounces for those, not for system emails.
declare -A CAMPAIGN_QIDS
tail -F "$LOG" 2>/dev/null | while IFS= read -r line; do
# Track queue IDs originating from campaign sender
if echo "$line" | grep -q "from=<noreply@performancewest.net>"; then
QID=$(echo "$line" | sed -n 's/.*postfix\/[a-z]*\[\([0-9]*\)\]: \([A-Z0-9]*\):.*/\2/p')
if [ -n "$QID" ]; then
CAMPAIGN_QIDS[$QID]=1
fi
fi
# Also track from=<info@performancewest.net>
if echo "$line" | grep -q "from=<info@performancewest.net>"; then
QID=$(echo "$line" | sed -n 's/.*postfix\/[a-z]*\[\([0-9]*\)\]: \([A-Z0-9]*\):.*/\2/p')
if [ -n "$QID" ]; then
CAMPAIGN_QIDS[$QID]=1
fi
fi
# Hard bounce: status=bounced
if echo "$line" | grep -q "status=bounced"; then
QID=$(echo "$line" | sed -n 's/.*postfix\/[a-z]*\[\([0-9]*\)\]: \([A-Z0-9]*\):.*/\2/p')
RCPT=$(echo "$line" | sed -n 's/.*to=<\([^>]*\)>.*/\1/p')
# Only report if this was a campaign email
if [ -n "$RCPT" ] && [ -n "${CAMPAIGN_QIDS[$QID]+x}" ]; then
curl -s -u "$AUTH" -X POST "$API/webhooks/bounce" \
-H "Content-Type: application/json" \
-d "{\"email\": \"$RCPT\", \"source\": \"postfix\", \"type\": \"hard\"}" \
>/dev/null 2>&1
logger -t bounce-notify "Hard bounce: $RCPT (qid=$QID)"
unset CAMPAIGN_QIDS[$QID]
fi
fi
# Soft bounce with permanent 5xx in the response
if echo "$line" | grep -q "status=deferred" && echo "$line" | grep -qE "said: 5[0-9][0-9]"; then
QID=$(echo "$line" | sed -n 's/.*postfix\/[a-z]*\[\([0-9]*\)\]: \([A-Z0-9]*\):.*/\2/p')
RCPT=$(echo "$line" | sed -n 's/.*to=<\([^>]*\)>.*/\1/p')
if [ -n "$RCPT" ] && [ -n "${CAMPAIGN_QIDS[$QID]+x}" ]; then
curl -s -u "$AUTH" -X POST "$API/webhooks/bounce" \
-H "Content-Type: application/json" \
-d "{\"email\": \"$RCPT\", \"source\": \"postfix\", \"type\": \"soft\"}" \
>/dev/null 2>&1
logger -t bounce-notify "Soft bounce (5xx): $RCPT (qid=$QID)"
fi
fi
# Clean up old QIDs when they're removed from queue
if echo "$line" | grep -q "removed$"; then
QID=$(echo "$line" | sed -n 's/.*postfix\/[a-z]*\[\([0-9]*\)\]: \([A-Z0-9]*\):.*/\2/p')
unset CAMPAIGN_QIDS[$QID] 2>/dev/null
fi
done