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, bro

Ahamed 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 lot

Uli (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 script

Julio 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. :P

David (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.

thanks

sys4dm1n (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) :
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...

David (29-04-2010 23:03) :
Awesome.

Was able to clean up over 100k spam emails from and to taiwan that we received over the past 8 hours.

Thanks!

brayan (10-06-2010 20:53) :
So NICE!! congratz man

koda (28-06-2010 12:32) :
Good job! :D

Thibs (04-08-2010 10:23) :
Excellebt ! Thanks you

Henry (24-09-2010 10:20) :
works Perfect as intended to do.

Thx.
Drib Ylrae

Kevin (25-11-2010 11:09) :
60,000 spam emails gone in a flash! Excellent work, thank you for sharing!!

Chuck (12-05-2011 08:37) :
Had a password cracked and they were using the account for spam. This made quick work of cleaning up the remaining garbage. Thanks!

Kristaps (25-05-2011 15:43) :
Thank you!

Sunshine (26-05-2011 15:05) :
Anyway to apply two ARGV instead of just 1 to the perl script ...

I am thinking variables where I could just hardcode in recipent and specific subject

To: XXXX@XXXX.XXX
Subject: I am out of office

eri (30-06-2011 13:54) :
I am very grateful you wrote this. God Bless You.

Gorav (19-09-2011 08:27) :
This was helpful

Clau (02-10-2011 22:44) :
Thanks for this script! It saved my day!

Raposo (17-10-2011 11:40) :
Thks!

Roshan (01-11-2011 10:00) :
im new to this. please explain where do i have to add this code. can i automatically Flush the Mail Queue

jackjohn (14-01-2012 09:32) :
roshan take alook at http://www.linfo.org/create_perl_1.html

I did these steps :
1. vi pfdel.pl
2. copy all the script.
3. execute script with this command
perl pfdel.pl thespammer@spammer.com

thank you for the script !!

Ethon (22-02-2012 05:46) :
Perfect! Saved my bacon on a hacked email account.

Linard (13-04-2012 14:24) :
Thanks!!

Robiul (19-09-2012 09:29) :
How to use it

Fawad (19-12-2012 11:14) :
Worked like a charm!!!
Thanks man

happy guy (13-03-2013 11:01) :
work like a charm
thank you!!!

calete (16-09-2013 21:22) :
One more thanks

Back to articles list

This page was last modified on 2024-12-05 17:05:37