Header RSS Feed
 
» IT tipps and howto's

How To: Delete specific mails from Mail Queue

Original Entry: 05.06.2008
Last Update: 05.03.2010 (see bottom)

I finally found a script, I was looking for forever! For a long time now I've been seeking for a script or a command which is able to delete only certain mails of the Mail Queue. And I finally found it today on this site: http://www.cyberciti.biz/tips/howto-postfix-flush-mail-queue.html

Here's a summary of what to do.
First of all, if you simply want to delete ALL the mails in your mailq, use the following simple command. Be sure to write ALL in capital letters.

postsuper -v -d ALL

Unfortunately there is no possibility to delete certain mails like to certain domains. For example: postsuper -d *.com << don't try that, it won't work (unfortunately). So what we need in this case, is a script, which searches through the mailq for a given domain and deletes only those mails.

This is the script which fulfils your dream, name it as you want, for example deletemail.pl:

#!/usr/bin/perl

my $REGEXP = shift(@ARGV) || die “no email-adress given (regexp-style, e.g. bl.*\@yahoo.com)!”;

my %Q;
my $queue_id;
my @data = `/usr/sbin/postqueue -p`;
foreach my $line (@data) {
if ($line =~ /^(\w+)(\*|\!)?\s/) {
$queue_id = $1;
}
if($queue_id) {
if ($line =~ /$REGEXP/i) {
$Q{$queue_id} = 1;
$queue_id = “”;
}
}
}

open(POSTSUPER,”|postsuper -d -”) || die “couldn’t open postsuper” ;
foreach my $key (keys %Q) {
print POSTSUPER “$key\n”;
}
close(POSTSUPER);

After you saved this file you can use the following command to delete mails from or to a certain domain (example host.com):

./deletemail.pl host.com

Updated 05.03.2010:

I got some difficulties with the script today - trying to use it on a SLES10 server. So I quickly wrote a oneliner in bash which does the same as this perlscript. Note that DOMAIN stands for the domain-name or the mail address you want to delete from the mailq. MONTH stands for the actual month in three letters (in this example Mar for March):

for i in $(mailq | grep DOMAIN | grep MONTH | awk '{ print $1 }') ; do postsuper -d $i; done

 


Go to Homepage home
Linux Howtos how to's
Monitoring Plugins monitoring plugins
Links links

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

6979 Days
until Death of Computers
Why?