Skip to content

Instantly share code, notes, and snippets.

@gh0stwizard
Created August 18, 2017 14:34
Show Gist options
  • Save gh0stwizard/e0d0daecac224305d83fa7eb40fea78f to your computer and use it in GitHub Desktop.
Save gh0stwizard/e0d0daecac224305d83fa7eb40fea78f to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Delete messages from postfix queue by specified MAILFROM & MAILTO
#
# Usage: clean-mail-queue.sh [MAILFROM] MAILTO
#
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH
mailfrom=$1
mailto=$2
if [[ $# -eq 0 ]]; then
echo "Usage: $0 [MAILFROM] MAILTO"
exit 1
fi
if [[ -n ${mailto} ]]; then
awkcmd=$(cat <<EOF
# $7=sender, $8=recipient1, $9=recipient2
BEGIN { RS = "" } { if (\$7 == "${mailfrom}" && \$8 == "${mailto}" && \$9 == "") print \$1 }
EOF
)
else
mailto=${mailfrom}
awkcmd=$(cat <<EOF
# $7=sender, $8=recipient1, $9=recipient2
BEGIN { RS = "" } { if (\$8 == "${mailto}" && \$9 == "") print \$1 }
EOF
)
fi
mailq | tail -n+2 | grep -v '^ *(' | awk "${awkcmd}" | tr -d '*!' | postsuper -d -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment