Skip to content

Instantly share code, notes, and snippets.

@olimortimer
Last active August 29, 2015 14:09
Show Gist options
  • Save olimortimer/e68ffe4aa8009a223c9d to your computer and use it in GitHub Desktop.
Save olimortimer/e68ffe4aa8009a223c9d to your computer and use it in GitHub Desktop.
Shell: Cleaning Email Queue
# Delete emails from the queue where the addresses or TO or FROM;
mailq | tail -n +2 | awk 'BEGIN { RS = "" } / name@domain\.co\.uk$/ { print $1 }' | tr -d '*!' | postsuper -d -
mailq | tail -n +2 | awk 'BEGIN { RS = "" } / domain\.co\.uk$/ { print $1 }' | tr -d '*!' | postsuper -d -
mailq | tail -n +2 | awk 'BEGIN { RS = "" } / MAILER-DAEMON@mail\.domain\.com$/ { print $1 }' | tr -d '*!' | postsuper -d -
# Find all IP addresses against the sasl_username and put them into iplist.txt
grep "sasl_username=name@domain.co.uk" /var/log/mail.log | sort -u > iplist.txt
# Go through all the IPs and delete the emails from the queue
grep -wFf /iplist.txt | cut -f 6 -d ' ' | cut -f 1 -d ':' | postsuper -d -
# Delete all MAILER-DAEMON emails from the queue
/bin/delmd
# Delete specific IP address from the queue
grep -lrE '[^0-9]10.20.30.4[^0-9]' /var/spool/postfix/deferred | xargs -r -n1 basename | postsuper -d -
# Delete by sasl_username - https://cms.arstechnica.com/civis/viewtopic.php?f=16&t=1203837
/bin/pfsasl name@domain.co.uk
# Delete emails by sender (case sensitive - ie, try hotmail.com, HOTMAIL.COM or HotMail.Com)
mailq|awk ' /^[0-9A-F][0-9A-F]*.*hotmail.com$/ {print $1}'|tr -d '*'| xargs -rn1 postsuper -d
# Delete emails by to address
mailq | tail -n +2 | awk 'BEGIN { RS = "" } { if ($8 == "names@domain.co.uk" && $9 == "")print $1 }' | tr -d '*!' | postsuper -d -
# Find out the script sending the emails
http://kb.sp.parallels.com/en/114845
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment