HOWTO: Remove mail from postfix queue based on from email or rcpto email address
Here is a simple perl script to delete messages from postfix queue based on email
#!/usr/bin/perl -w
#
# pfdel - deletes message containing specified address from
# Postfix queue. Matches either sender or recipient address.
#
# Usage: pfdel <email_address>
#
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 <email_address>\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 = <QUEUE>; # skip single header line
$/ = ""; # Rest of queue entries print on
# multiple lines.
while ( $entry = <QUEUE> ) {
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;
Comments:
tyamadath (12-12-2007 19:56) :
thks man!tsikamtanda (07-03-2008 09:55) :
wonderful!
Good job, broAhamed Bauani (14-03-2008 12:28) :
Thanks for this Smple, Speedy and Small Script, which is saving my time of administrating my SMTP & MX server which transfer about 10,000 emails per day.
Thanks Again
http://www.bauani.org/manfred (02-05-2008 12:17) :
Thanks a lotUli (02-05-2008 18:52) :
Thank you so much!
This is great!chrisv (18-05-2008 22:19) :
worked like a charm - thanks!Jay Canfield (19-05-2008 22:57) :
Thanks for this. We had an app go screwy and send about 300k emails to our relay. Fixed it right up.Fredy Coutinho (23-07-2008 18:35) :
Thanks a lot! this work fine!!!JM (10-10-2008 23:05) :
Slick, but can this support any sort of wildcards? Or only explicit, exact match?
Frex., we have dozens of mail messages continually being tried to noreply@* sorts of addresses, but I don't know how to blanket them all with a wildcard, or if I could.
RT (24-11-2008 07:32) :
Perfect simple script.
Thanks :)SG (17-12-2008 13:45) :
Thanks, nice scriptJulio del Aguila (01-01-2009 06:17) :
Thanks man, It's so useful.luisxvi (02-01-2009 16:42) :
It makes me feel like terminator! Nice script. Thanks.izzol (17-02-2009 17:51) :
It Worked :) Tkanks!.Murray (04-03-2009 07:13) :
Wonderful work. Just what I needed.Madison Kelly (25-03-2009 20:59) :
Thanks for the script. Helped me clear a bunch of queued messages I screwed up by putting the wrong TLD in a script's from field. :PDavid (28-04-2009 13:50) :
Thanks a lot ... your great script saved me a lot of time !!!Marco_mimo05 (14-05-2009 12:35) :
Grazie mille sei un figo !! il tuo script mi è stato utilissimo
Thanks man you r an ace !! your script is really usefull.
Arthur (10-07-2009 17:15) :
Thank you very much for the script! It works great! I've put it on cron on my server. It helps me to remove junk mails ;)
norman (08-09-2009 10:41) :
thanks man .. !Indhran (16-09-2009 04:20) :
Thank you very much.great job.Al (05-11-2009 13:58) :
This saved me from a flood of Italian spam, thanks!Satish (12-11-2009 10:45) :
I just loved the script, Thanks for the geek who wrote the script, it saved my day.Sniffer Networks do brasil (01-12-2009 16:42) :
Works like charm.
thankssys4dm1n (17-12-2009 23:32) :
Good stuff! Thank you!LaffyTaffy (14-01-2010 01:35) :
Great Script. Well done.James Su (26-01-2010 13:27) :
Thanks!, it works fine!Moritz (05-03-2010 19:52) :
Post a comment (fixed now)
Dude! Thanks!!! My system got hosed by that brazilian zencart exploit, its nice to clean out the junk in the queue this way, works much better...
This page was last modified on 2010-03-10 21:23:05