The linux way to do things

Alert users when they are about to exceed their quota using Courier-IMAP

I recently built a Postfix/Courier-IMAP server with virtual domains that uses maildirquota to enforce quotas limitation.  Migrating from a H-Sphere setup, I couldn’t ignore the fact that despite all its flaws, it had a very useful built-in feature: it could send an email to a user when this one was about to exceed its quota.

Since the machine was now offline, meaning I couldn’t find anymore the script executing this task, I decided, after quickly looking on the internet for one, to write my own.  Here it is in Bash :

Note that this script uses mutt since I am usually sending HTML emails.  Should you be fine with text emails, you can use the mail command.



for dir in /var/mail/vhosts/*
    for mailbox in $dir/*
        rawQuotaSize=$(head -n1 $mailbox/maildirsize 2>/dev/null)
        if [ ! -z ${#rawQuotaSize} ]; then
            alertQuotaSize=$(($quotaSize * $ratio / 100))
            mailBoxSize=$(du -sb $mailbox | cut -d"/" -f1)
            if [ $mailBoxSize -gt $alertQuotaSize ]; then
                email=$(echo "$mailbox" | cut -d/ -f6)@$(echo "$mailbox" | cut -d/ -f5)
                echo "Optional email body" | mutt -s "You are now at $ratio% of your
 allowed storage capacity" -e "set content_type=text/html" 
-e "set realname = \"donotreply\"" $email

The script is self-explanatory: it loops through each mailbox of each virtual domain and for each, get the mailbox’s quota.  If a quota is set, it then check if the current size of mailbox is greater than the ratio * the mailbox quota.  If so, an email is sent.


Be Sociable, Share!

Categorised as: Bash

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>