APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version


Keep a watchful eye on email delivery problems with this script

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);
$count=@delayed;
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; 
} 
} 
 

Kerio®, and related trademarks, names and logos are the property of Kerio Technologies, Inc. and are registered and/or used in the U.S. and other countries. Used under license from Kerio Technologies, Inc.


If this page was useful to you, please help others find it:  





1 comment




More Articles by Anthony Lawrence - Find me on Google+



Click here to add your comments





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

gravatar


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



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar


Kerio Connect Recovery

Kerio Connect Mailserver

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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Kerio
       - Kerio Info
       - Kerio Pricing
       - Kerio RSS Feed
       - Mail
       - Perl
       - Scripting


















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals