In larger organizations, your smarthost/default SMTP gateways mailq can become overloaded with unwanted and undeliverable messages. If you have enough of them, it will add a lot of CPU and disk IO load as it tries to resend/reprocess Undeliverable messages. We have a few systems that if left unchecked would try and reprocess 100K messages every 5 min. The tmp mail files are typically located /var/spool/mqueue and directly deleting these is not the correct manner and opening each one to verify deletion will likely drive you mad. I have added four scripts that will help you manage the stagnant mail you may have accumulating in your mailq. These scripts will list the mail per target address, allow you to delete mail for a single user or an entire domain.
Using the below script “who-mail.sh”, you can determine what is causing the mailq to fill, which SMTP address and how many email messages exist in your mailq for delivery. (All of these tools can be downloaded from here)
#!/usr/bin/perl
use strict;
my $mqueue_directory = "/var/spool/mqueue";
my %occurrences;
use File::Find;
# Recursively find all files and directories in $mqueue_directory
find( \&wanted, $mqueue_directory );
sub wanted {
# Is this a qf* file?
if (/^qf\w{14}/) {
open( QF_FILE, $_ );
while () {
# Lines beginning with R contain an envelope recipient
if (/^R.*:<(.*)>$/) {
my $domain = lc($1);
# Add 1 to the %occurrences hash
$occurrences{$domain}++;
}
}
}
}
# Subroutine to sort hash by ascending value
sub hashValueAscendingNum {
$occurrences{$a} <=> $occurrences{$b};
}
# Print sorted results
foreach my $key ( sort hashValueAscendingNum ( keys(%occurrences) ) ) {
print "$occurrences{$key} $key\n";
}
This prints to screen the qty and full email address of messages in queue. Looking at the bottom few rows you will typically find your main source of mailq stress.