QueuedByAddr

From My Admin Page
Jump to: navigation, search

The following Perl script will show all queued mail by email address. A message may be counted more than once if it has multiple envelope recipients.

#!/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(<QF_FILE>) {
         # 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";
}