Keep a watchful eye on email delivery problems with this script


2012/09/06

Sometimes email cannot be delivered. If that's because of a mis-typed address (gmail.cim, for example), the sending user finds out about it very quickly and hopefully realizes what is wrong without your assistance.

At other times, the receiving email server may be temporarily down. In that case, your server will keep trying for some period of time (usually a few days at least) and will notify the user that their email may be delayed.

You can change how quickly users are notified and how long your Kerio server will attempt delivery in Configuration->SMTP Server->Queue Options.

However, Connect only sends one warning. As you probably want to send that warning fairly quickly, but also want to wait a reasonable length of time before giving up, there can be a long period of time before a user gets notified of actual failure to deliver.

Perhaps more importantly, your user may not fully understand the initial notice. They may assume that they'll be getting an update when the message is sent, but that's not the case. They might also assume that they'll get updates on continuing problems, but that won't happen either.

You, as the Administrator, can look at the Kerio Mail Queue whenever you like and could be well aware of these delays.

But.. these issues are not common. Unless you run a very large mail server or are unusually conscientious, you probably don't look at this very often because most of the time there is nothing worth looking at! Delivery delays are at least somewhat rare and usually are self-correcting.

Notify Script

So why not write a little script to notify us and the users of stuck mail? I've done that here, and while my script is a bit rough around the edges, it seems to do the job. I've commented it heavily and noted places you could modify it. Please do let me know of any bugs!

A couple of notes:

You could extract admin and store data from the Connect configuration files, of course.

This uses the undocumented method of writing an eml file to the root of the user directory in Store. The server will eventually move any such files to the user's INBOX, but you can hasten the process by logging out and logging back in.

You could actually mail the messages, but that might require Perl libraries you don't have - this script requires nothing unusual.

I'd run this perhaps once a day at most, manually or with cron.

I think it will work fine on Windows but I have not tested that.

#!/usr/bin/perl
$STOREDIR="/usr/local/kerio/mailserver/store/";
# Modify above for your Store directory
$ADMIN="admin\@localhost";
# Your admin to be notified - don't forget to eacape the @ !
$MAXNOTIFY=50;
# Don't notify users if the total count of delays exceeds this 
# Assumption is that something is very wrong and no mail is going out if exceeded
$AGE=2 * 60 * 60  ;
# How old does it have to be (seconds) before we notify?
$FIRSTEML=1;
# You don't need to change this unless you know you are writing 
# other eml's directly to mailboxes - you likely are NOT.
# nothing beow here should need modification (though you certainly can!)
$now=time();
@delayed=();

chdir("$STOREDIR/queue") or die "Can't reach $STOREDIR/queue $!";;
opendir(Q,".") or die;
while (defined($file=readdir(Q))) {
  next if $file=~ /^\.\.?$/;
  next if $file=~ /pickup/;
  foreach(<$file/*.eml>) {
   @s=stat($_);
   next if ($s[10] + $AGE) > $now;
   $envelope=$_;
   $envelope=~ s/.eml$/.env/;
# We look at the eml date because the env file gets updated
# The env file has what we really want
   open(ENV,"<:crlf",$envelope) or next;
   # might not be there if queue is corrupt
   $status="Unknown";
   $sentby="";
   $asentby="";
   $retry=""; 
   $sentto="";
   while(<ENV>) {
     chomp;
     $status="" if /<status>/;
     $status=$_ if /<status-text/;
     $sentto=$_ if /<orig-name>/;
     $sentby=$_ if /<auth-sender>/;
     $asentby=$_ if /<sender>/;
     $retry=$_ if /<retry>/;
   }
  $sentby=~ s/\s*<.?auth-sender>\s*//g;
  $asentby=~ s/\s*<.?sender>\s*//g;
  $sentby=$asentby if not $sentby;
  $sentby=$asentby if $aentby =~ /Unknown/;
  $sentto=~ s/\s*<.?orig-name>\s*//g;
  $retry=~ s/\s*<.?retry>\s*//g;
  $retrytime=scalar localtime($retry);
  $status=~ s/^\s*//;
  push @delayed,  "$sentby|$sentto|$status|Will retry $retrytime";
  }
}
closedir(Q);
[email protected];
exit 0 if not $count;
chdir("$STOREDIR");
 ($adminuser,$admindomain)=split /\@/,$ADMIN;
 $eml=sprintf("$STOREDIR/mail/$admindomain/$adminuser/%.8x.eml",$FIRSTEML++);
 open(ADMIN,">$eml") or print "$! $eml\n";;
 print ADMIN <<EOF;
Subject: Delayed email
From: $ADMIN
To: $ADMIN

EOF
# Yes, we are always writing to the same file.  It's possible that an older file may be 
# overwritten before tha admin sees it - but so what?  This is CURRENT status.
# Same concept applies to user notifications.
foreach (@delayed) {
   s/\|/ /g;
   print ADMIN "$_\n";
}
close ADMIN;
if ($count < $MAXNOTIFY) {
 foreach (@delayed) {
   @d=split / /;
   ($user,$domain)=split /\@/,$d[0];x;
   $eml=sprintf("$STOREDIR/mail/$domain/$user/%.8x.eml",$FIRSTEML++);
   open(DELAYED,">$eml") or print "$! $eml\n";
   print DELAYED <<EOF;
Subject: Delayed email
From: $ADMIN
To: $delay[0]

The following message is delayed.  
$_
EOF

}
}

If you'd prefer to use mail, this will get you going:

 
 
open(SEND,"|/opt/kerio/mailserver/sendmail $ADMIN"); 
print SEND <<EOF; 
Mime-Version: 1.0 
Content-Type: text/html 
Content-Transfer-Encoding: 8BIT 
Subject: Delayed email 
From: $ADMIN 
 
EOF 
foreach (@delayed) { 
   s/\|/ /g; 
   print SEND "$_<br />\n"; 
   print  "$_\n" if $DEBUG; 
} 
close SEND; 
 
if ($count < $MAXNOTIFY) { 
 foreach (@delayed) { 
@d=split / /; 
open(SEND,"|/opt/kerio/mailserver/sendmail $d[0]"); 
print SEND <<EOF; 
Mime-Version: 1.0 
Content-Type: text/html 
Content-Transfer-Encoding: 8BIT 
Subject: Delayed email 
From: $ADMIN 
 
$_ 
EOF 
 
close SEND; 
} 
} 
 


Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Keep a watchful eye on email delivery problems with this script


1 comment



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Thu Sep 6 19:14:28 2012: 11274   TonyLawrence

gravatar


The script could use some error checking - keep that in mind, please.



------------------------
Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





C is quirky, flawed, and an enormous success. (Dennis Ritchie)

Just because they've sold you an IP based phone system doesn't mean they know anything about IP, does it? (Tony Lawrence)








This post tagged: