Bash Script for Spam Queue Removal on Zimbra Mail Server (Updated)

I have a small script-got it from Internet-for Zimbra spam queue removal based on Postfix queue message removal. The script written on Perl language and become a mandatory script on each Zimbra server deployment, as a quick tools to remove spam message from queue caused by compromised account.

image from pixabay

As of Zimbra 8.7, there is a little change on path for some Postfix binary. On previous version, Postfix binary located on /opt/zimbra/postfix/ but since version 8.7, the binary folder moved into /opt/zimbra/common/ so I take a little time to change the script.

Below is an updated script, check for Zimbra version and change binary folder accordingly :

#!/usr/bin/perl -w
#
# pfdel - deletes message containing specified address from
# Postfix queue. Matches either sender or recipient address.
#
# Usage: pfdel sender-or-recipient-address
#

use strict;

my $LISTQ;
my $POSTSUPER;

# Change these paths if necessary.

system("su - zimbra -c 'zmcontrol -v' > /tmp/zcs-ver.txt");
my $VER=`awk '/Release 6|Release 7|Release 8.0|Release 8.5|Release 8.6/' /tmp/zcs-ver.txt`;
#print $VER

if (length($VER) > 0)
{
$LISTQ = "/opt/zimbra/postfix/sbin/postqueue -p";
$POSTSUPER = "/opt/zimbra/postfix/sbin/postsuper";
}
else
{
$LISTQ = "/opt/zimbra/common/sbin/postqueue -p";
$POSTSUPER = "/opt/zimbra/common/sbin/postsuper";

}

my $email_addr = "";
my $qid = "";
my $euid = $>;

if ( @ARGV != 1 ) {
die "Usage: pfdel \n";
} else {
$email_addr = $ARGV[0];
}

if ( $euid != 0 ) {
die "You must be root to delete queue files.\n";
}

open(QUEUE, "$LISTQ |") ||
die "Can't get pipe to $LISTQ: $!\n";

my $entry = ; # skip single header line
$/ = ""; # Rest of queue entries print on
# multiple lines.
while ( $entry = ) {
if ( $entry =~ / $email_addr$/m ) {
($qid) = split(/\s+/, $entry, 2);
$qid =~ s/[\*\!]//;
next unless ($qid);

#
# Execute postsuper -d with the queue id.
# postsuper provides feedback when it deletes
# messages. Let its output go through.
#
if ( system($POSTSUPER, "-d", $qid) != 0 ) {
# If postsuper has a problem, bail.
die "Error executing $POSTSUPER: error " .
"code " . ($?/256) . "\n";
}
}
}
close(QUEUE);

if (! $qid ) {
die "No messages with the address <$email_addr> " .
"found in queue.\n";
}

exit 0;

If you tend to lazy to copy-paste the script, get it from mine :

wget https://www.vavai.com/pfdel
chmod +x pfdel

And to run it, just run this script with root permissions :

./pfdel email-spammer

ex :

./pfdel spammersampah@vavai.com

However, this is only a quick tools to remove spam, but more importantly is prevent spam from being filling into queue.

Leave a Reply