Couple of scripts I used while cleaning up a mail server. I’m sure they can be improved, and the last one is quite specific to my own requirements, but I’ll put them here anyway.
Move emails with a particular subject from the hold queue to the deferred queue:
#change directory to postfix's queue directory# cd $(postconf -h queue_directory)/hold #loop over queue files for i in * ; do # postcat e file, grep for subject "test" and if found # run postsuper -d to delete queue'd message postcat $i |grep -q '^Subject: test' && postsuper -H $i done
Delete emails in the hold queue that are being sent to a recipient that has already recieved an email (is in the mail log) or duplicate emails (with the same email/subject):
cd $(postconf -h queue_directory)/hold #loop over queue files NUM=0 for i in * ; do if [ -f "$i" ]; then IDENT=$(postcat $i | grep -A 1 "To:") RECIPIENT=$(postcat $i | grep "To:" | cut -c 5- ) if grep -q "$RECIPIENT" /root/postfixtmp/logs/mailsent.log; then echo "* already sent to $RECIPIENT, deleting $i " | tee -a /root/postfixtmp/queueclean.log echo $IDENT | tee -a /root/postfixtmp/queueclean.log NUM=$[NUM + 1] postsuper -d $i echo "----" | tee -a /root/postfixtmp/queueclean.log else for o in * ; do if [ -f "$o" ]; then if [ $o != $i ]; then CURRENT=$(postcat $o | grep -A 1 "To:") if [ "$CURRENT" = "$IDENT" ]; then echo " duplicate email, deleting $o *" | tee -a /root/postfixtmp/queueclean.log echo $CURRENT | tee -a /root/postfixtmp/queueclean.log NUM=$[NUM + 1] postsuper -d $o echo "----" | tee -a /root/postfixtmp/queueclean.log fi fi fi done fi fi done echo "Deleted $NUM emails" | tee -a /root/postfixtmp/queueclean.log