Author Topic: HOWTO Postfix Quota Notification Email Script  (Read 8160 times)

bluekrash

  • Zen Apprentice
  • *
  • Posts: 7
  • Karma: +1/-0
    • View Profile
HOWTO Postfix Quota Notification Email Script
« on: June 04, 2010, 05:08:15 pm »
Hi i found this how to for create Postfix Quota Notification Email Script and i adapted for use with EBOX and i want to share with yours

1. Create file called script mail_quota_notify

2. with this code
Code: [Select]
#!/usr/bin/perl -w

use strict;

my $POSTFIX_CF = "/etc/postfix/main.cf";
my $MAILPROG = "/usr/sbin/sendmail -t";
my $WARNPERCENT = 80;
my @POSTMASTERS = ('postmaster@domain.com');
my $CONAME = 'Put your support area name';
my $COADDR = 'mailforsuppot@domain.com';
my $SUADDR = 'mailforsuppot@domain.com';
my $MAIL_REPORT = 1;
my $MAIL_WARNING = 1;

#get virtual mailbox base from postfix config
open(PCF, "< $POSTFIX_CF") or die $!;
my $mboxBase;

while (<PCF>) {
next unless /virtual_mailbox_base\s*=\s*(.*)\s*/;
$mboxBase = $1;
}
close(PCF);

#assume one level of subdirectories for domain names
my @domains;
opendir(DIR, $mboxBase) or die $!;
while (defined(my $name = readdir(DIR))) {
next if $name =~ /^\.\.?$/; #skip '.' and '..'
next unless (-d "$mboxBase/$name");
push(@domains, $name);
}
closedir(DIR);

#iterate through domains for username/maildirsize files
my @users;
chdir($mboxBase);
foreach my $domain (@domains) {
opendir(DIR, $domain) or die $!;
while (defined(my $name = readdir(DIR))) {
next if $name =~ /^\.\.?$/; #skip '.' and '..'
next unless (-d "$domain/$name");

push(@users, {"$name\@$domain" => "$mboxBase/$domain/$name"});


# Added to show if the path to the mailboxes are correct.
# At this point, its showing all the correct path to each mailbox
printf($mboxBase."/".$domain."/".$name."\n");


}
}
closedir(DIR);

#get user quotas and percent used
my (%lusers, $report);
foreach my $href (@users) {
foreach my $user (keys %$href) {
my $quotafile = "$href->{$user}/maildirsize";
next unless (-f $quotafile);
open(QF, "< $quotafile") or die $!;
my ($firstln, $quota, $used);
while (<QF>) {
my $line = $_;
if (! $firstln) {
$firstln = 1;
die "Error: corrupt quotafile $quotafile"
unless ($line =~ /^(\d+)S/);
$quota = $1;
last if (! $quota);
next;
}
die "Error: corrupt quotafile $quotafile"
unless ($line =~ /\s*(-?\d+)/);
$used += $1;
}
close(QF);
next if (! $used);
my $percent = int($used / $quota * 100);
# $lusers{$user} = $percent unless not $percent;
$lusers{$user} = $percent;
}
}

#send a report to the postmasters

#send a report to the postmasters
if ($MAIL_REPORT) {
open(MAIL, "| $MAILPROG");
select(MAIL);
map {print "To: $_\n"} @POSTMASTERS;
print "From: $COADDR\n";
print "Subject: Daily Mail Quota Report.\n";
print "DAILY MAIL QUOTA REPORT:\n\n";
print "----------------------------------------------\n";
print "| % USAGE in %| ACCOUNT NAME |\n";
print "----------------------------------------------\n";
foreach my $luser ( sort { $lusers{$b} <=> $lusers{$a} } keys %lusers ) {
printf("| %3d | %32s |\n", $lusers{$luser}, $luser);
print "---------------------------------------------\n";
}
print "\n--\n";
print "$CONAME\n";
close(MAIL);
}

#email a warning to people over quota
if ($MAIL_WARNING) {
foreach my $luser (keys (%lusers)) {
next unless $lusers{$luser} >= $WARNPERCENT; # skip those under quota
open(MAIL, "| $MAILPROG");
select(MAIL);
print "To: $luser\n";
map {print "BCC: $_\n"} @POSTMASTERS;
print "From: $SUADDR\n";
print "Subject: WARNING: Your mailbox is $lusers{$luser}% full.\n";
print "Reply-to: $SUADDR\n";
print "Your mailbox: $luser is $lusers{$luser} full.\n\n";
print "Once your e-mail box has exceeded your quota. You can't send or receive emails\n";
print "Please consider deleting e-mail and emptying your trash folder to clear some space.\n\n";
print "Contact <$SUADDR> for further assistance.\n\n";
print "Thank You.\n\n";
print "--\n";
print "$CONAME\n";
close(MAIL);
}
}

3. set chmod 755

4. copy to /etc/cron.daily  (to get daily email report with all the informations of the email accounts quota and notify user with over 80% quota capacity)

in code:

on the top set with your own parametters $WARNPERCENT $POSTMASTERS $CONAME

after line #email a warning to people over quota
you can change EMAIL MSG (subject, body etc..)

credits to asyadiqin
http://www.howtoforge.com/forums/showthread.php?t=10883
« Last Edit: June 04, 2010, 05:10:55 pm by bluekrash »

sixstone

  • Zentyal Staff
  • Zen Hero
  • *****
  • Posts: 1417
  • Karma: +26/-0
    • View Profile
    • Sixstone's blog
Re: HOWTO Postfix Quota Notification Email Script
« Reply #1 on: June 04, 2010, 07:21:09 pm »
Thanks for sharing this with us :)
My secret is my silence...

ctek

  • Zen Warrior
  • ***
  • Posts: 197
  • Karma: +6/-1
    • View Profile
Re: HOWTO Postfix Quota Notification Email Script
« Reply #2 on: June 04, 2010, 07:36:26 pm »
Very Nice!.
Maybe this could be added by default into Web interface with some checks and the appropriate fields.
Thank you for sharing ! :)

Best regards
Bogdan

flashbios

  • Zen Apprentice
  • *
  • Posts: 36
  • Karma: +0/-0
    • View Profile
    • Electronics, Bioschip & WebDesign services
Re: HOWTO Postfix Quota Notification Email Script
« Reply #3 on: June 04, 2010, 07:54:57 pm »
Thanks !!!!

Allways happy when people create and share Howto's !!!!

This is a good way to help the Ebox-company to improve their great software !!!