Wednesday, December 23, 2009

Clenup Email Queue on Postfix Mail Server based on From or Recepent (RCPT) address


Removing Queued Email from Postfix queue based on From or Recipient (rcpt) address.


We, administrators of Postfix mail server sometime need to remove email based on From address or Recipient (RCPT) address and we know how much time can kill when any virus / spam email / warm etc relay email on SMTP servers. I found a easy script to remove all targeted email from queue. Here is that script, writen on perl:



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

use strict;

# Change these paths if necessary.
my $LISTQ = "/usr/sbin/postqueue -p";
my $POSTSUPER = "/usr/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;

### End of Script ####

I don't know the name of the writer of script above. But found in URL:

http://www.ustrem.org/en/articles/postfix-queue-delete-en/

Use it and hope it will save your times. Good Day.

0 comments:

Post a Comment

Other Posts