1. Please take a little time for this simple survey! Thank you for participating!
    Dismiss Notice
  2. Dear Pleskians, please read this carefully! New attachments and other rules Thank you!
    Dismiss Notice
  3. Dear Pleskians, I really hope that you will share your opinion in this Special topic for chatter about Plesk in the Clouds. Thank you!
    Dismiss Notice

Qmail e-mail quota notification script for Plesk 9.3+/10+

Discussion in 'Plesk 10.x for Linux Issues, Fixes, How-To' started by Scy, Dec 2, 2010.

  1. Scy

    Scy Basic Pleskian

    23
    73%
    Joined:
    Feb 8, 2009
    Messages:
    67
    Likes Received:
    0
    Hi,

    I was long looking for a suitable script how to prevent our customers to overuse their mailbox quota. The problem is that many of the clients don't pay attention to quota limit mentioned on the webmail and/or are using other e-mail clients like Outlook or Thunderbird and won't see the quota limit at all, thus resulting full used quota and blocked e-mail and "mail quota exceeded" notifications.

    To prevent this, many of our clients have requested a some kind of notification to mailbox users when mailbox quota is about to reach it's limit.

    I remember that there were certain bash shell scripts provided, that would check every mailbox on Plesk server and in case the certain percentage of the quota is overused, the script would notify the mailbox user to delete some messages before the quota limit is exceeded and mailbox gets stucked and won't receive any more e-mails.

    One of this kind of script was provided here couple of years ago: http://forum.parallels.com/showthread.php?t=71666

    However, the problem with this old scripts is that they're designed for olded Plesk's / Qmails where mailbox quota was measured with different method than on the current branch of Plesk 9.3+/Plesk 10+ & Qmail. (Also some other older script required to include admin login & password inside the script, which I felt really risky.)

    I decided to modify this first script (provided by azur99) to make it work with newer Plesk/Qmail. I needed to find a way to read quota from maildirsize file located in Maildir folder. I found from the some other thread that the first set of numbers in the first line should be the whole quota for the mailbox - and the sum of the other lines (first set of numbers per every line) should total the used quota. (This should not be read with "du" command, like in the original script, as it does not give exact result of the used quota, but instead the used disk space on the disk system of the same folder, which can be a lot different.) Comparing these two values calculated from the maildirsize file should give the percentage close enough.

    In this case I also noticed that mailbox quota value is missing 4096 bytes, that is being added to the hardquota parametre - but the used quota values are missing some variating amount of bytes. This is why the percentage is some small fraction smaller than the percentage shown by Horde IMP webmail. However the difference is so small that it doesn't matter.

    DISCLAIMER! Please notify that using this script is solely on your own responsibility. If it works, be happy. If it doesn't or causes any damage to your server, clients or your businesses, it's on your own problem. I won't take any responsibility for the possible problems caused. Using this happens on your own risk!

    The script has been briefly tested to work with Debian Lenny 5.0 / Plesk 9.5.3 and Qmail, but it may work also on other configurations of Plesk 9.3+/10+ with Qmail. Also please notify that this script has not been tested on the long rung, so I cannot quarantee does it work with all cases (as it seems that maildirsize seems to grow lines with every received/deleted e-mail and all of these lines must be read with tail command and sum with paste/bc commands. I have no experience whatsover of this script in the long run, however it seems to work with our servers having 600+ domains / 1500+ mailboxes per server.)

    Anyways here's the script and the instructions how to set it:

    1. Copy and paste the followin script to notepad/pico or any other favorite editor of your's and save the file with name "mailboxquota.sh". (You may need to modify the Plesk installation path MAILROOT=/var/qmail/mailnames):

    Code:
    #! /bin/sh
    # Mail quota reaching it's limit e-mail notification
    # script for Plesk 9.3+ / Plesk 10 & Qmail
    # Modified by "Scy" from the original script
    # provided by "azur99" in Plesk forum:
    # http://forum.parallels.com/showthread.php?t=71666
    #setenv QMAILUSER 'do-not-reply'
    MAILROOT=/var/qmail/mailnames
    cd $MAILROOT > /dev/null
    for DIR in *.*;do
            cd $MAILROOT/$DIR
            for MAILBOX in * ;do
                    if [ -d $MAILBOX ]
                            then
    
                            # look for specific mailbox quota file and set mailbox softquota
                            QUOTAFILE=$MAILROOT/$DIR/$MAILBOX/Maildir/maildirsize
    			
    			# Fetching mailbox quota size in bytes
    			HARDQUOTA=$((`head -1 $QUOTAFILE | cut -d S -f1`))
    			HARDQUOTA=$(($HARDQUOTA+4096))		
    
    			# Fetching space used by mailbox in bytes
    			MBOXSPACE=$((`tail -n +2 $QUOTAFILE | cut -c1-12 | paste -sd+|bc`))
    
    			# Calculate the quota limit required for mail warning (95% for default)
    			SOFTQUOTA=$((95 * $HARDQUOTA / 100))
    
    			# Calculate mailbox usage percentage (with two decimals)
    			MBOXPERCENT=$(echo "scale=2; $MBOXSPACE*100/$HARDQUOTA" | bc)
    
    			# Check if the mailbox is full enough for warning, and if, send the warning mail
                if [ $HARDQUOTA -gt 0 -a $MBOXSPACE -gt $SOFTQUOTA ]; then
    			
    				# Let's generate the values in megabytes (with two decimals)
    				HARDQUOTA=$(echo "scale=2; $HARDQUOTA/1048576" | bc)
    				if [ "$(echo $HARDQUOTA | cut -c1)" = "." ] ; then HARDQUOTA="0"$HARDQUOTA
    				fi
    				MBOXSPACE=$(echo "scale=2; $MBOXSPACE/1048576" | bc)
    				if [ "$(echo $MBOXSPACE | cut -c1)" = "." ] ; then MBOXSPACE="0"$MBOXSPACE
    				fi
    			
    mail -s "E-mail quota warning!" $MAILBOX@$DIR -a "Content-Type: text/plain; charset=ISO-8859-1" << EOF
    Dear mailbox user,
    
    Your e-mail '$MAILBOX@$DIR' is about to reach its maximum quota and is already using $MBOXSPACE MB ($MBOXPERCENT%) out of the maximum quota $HARDQUOTA MB.
    
    We would kindly suggest you to delete some older messages and purge them to free some disk space for mailbox. In case the quota limit is reached, you won't be able to receive any new messages and the sender will receive  'mail quota exceeded' notifications.
    
    The other option is to configure POP3 mail client (e.g. Microsoft Outlook, Mozilla Thunderbird or Mac Mail) that would empty the server mailbox every time the mail account is read and move the old mail to local disk of your own computer.
    
    PS. This is an automated message and should not be replied. If you require any assistance, please contact to your mail server provider.
    EOF
    
    			fi
    
    
    				fi
            done;
    
    done;
    
    2. Transfer the file to path /usr/local/psa/bin/ in your server (or whatever the psa installation path is) and chmod the file to executable 0755.

    3. Add following root crontab (crontab -e) to run the script once in every day on midnight (make sure the code is on single line only):

    Code:
    00      0       *       *       *       /usr/local/psa/bin/mailboxquota.sh > /dev/null 2>&1
    So the script should be automatically ran every midnight and send notification to every mailbox that is using at least 95% of it's mail quota. In case you want to modify that value, feel free to do so in the line SOFTQUOTA=$((95 * $HARDQUOTA / 100))

    Also the values are modified to megabytes for the notification. If you want to use kilobytes instead, modify the two instances of $HARDQUOTA/1048576 to $HARDQUOTA/1024 and all MB's to kB's in the email template.

    In my Debian system I was unable to change the sender mail address with setenv QMAILUSER 'do-not-reply' line, which why it's commented. In case it work's with you, you can uncomment it and modify mail -s to mail -r do-not-reply@$DIR -s.

    Also a word about Plesk 9/10 mail quota calculation bug: We have noticed that from some version on the Plesk 9 branch mailbox quota calculation changed and deleted messages weren't no longer counted in the total used mailbox quota. IMHO this behaviour is wrong as it allows users to mark mail as deleted freeing the mailbox quota, withouth purging any deleted mails. This how the customers can continue filling the mailbox dir as the marking some mail as deleted is enough to free the quota. We have requested a fix for this issue (so that also mails that are deleted but not purged are also calculated in the mailbox used quota, and we received the fixed imapd and deliverquota files for 32-bit/64-bit Debian to fix this issue.) However, it seems that issue is still persisting in Plesk 9.5.3/10.0.1 so I hope Parallels will fix it in the future versions of Plesk.

    Feel free to give me any feedback and comments about the script. Also any bugs found and reported to here are appreciated, however I won't promise to fix anything. I just felt I wanted to share this script to other Plesk admins and hope that they would benefit from it. Have a nice day! :)
     
  2. lhwparis

    lhwparis Guest

    0
     
    thank you very much i ll try this script soon. if it works i ll use it:)
     
  3. Faris Raouf

    Faris Raouf Silver Pleskian Plesk Guru

    31
    30%
    Joined:
    Mar 15, 2009
    Messages:
    667
    Likes Received:
    17
    I've not tried this but it looks very good and is VERY useful. Thank you for your contribution.
     
  4. lhwparis

    lhwparis Guest

    0
     
    Anyway to get something like this in one of the next Plesk versions as a limit-notification with changeable text? would be very useful
     
  5. ReneDiep

    ReneDiep New Pleskian

    16
    30%
    Joined:
    Feb 21, 2011
    Messages:
    12
    Likes Received:
    0
    Added a few lines for unlimited quota

    This script works also for postfix ( because of the location of the mail files : plesk uses /var/qmail/mailnames also for postfix )

    I came across two bugs:

    The shebang ( first line of the script ) has to be #!/bin/bash for me to work.
    I take it the system where this script was run on had a symlink /bin/sh -> /bin/bash

    With unlimited quota, users get a mail with numbers like 16422% overusage.
    I fixed this with a few lines:

    # Fetching mailbox quota size in bytes
    HARDQUOTA=$((`head -1 $QUOTAFILE | cut -d S -f1`))
    if [ "$HARDQUOTA" -eq 0 ]; then
    continue
    fi

    Thanks for the script!
     
  6. lhwparis

    lhwparis Guest

    0
     
    thanks for your fixes!
     
  7. lhwparis

    lhwparis Guest

    0
     
  8. tomaton

    tomaton Guest

    0
     
    Last edited by a moderator: Jun 18, 2012
  9. supportn

    supportn New Pleskian

    6
     
    Joined:
    Mar 25, 2013
    Messages:
    1
    Likes Received:
    0
    script works perfect for 10.4. on debian squeeze. thanks!
     
  10. akabuki

    akabuki New Pleskian

    6
     
    Joined:
    Apr 24, 2013
    Messages:
    2
    Likes Received:
    0
    I had error "bc command not found" syntax error????
     
  11. technikm

    technikm New Pleskian

    12
    55%
    Joined:
    Jan 25, 2012
    Messages:
    3
    Likes Received:
    0
    most likely you have to install it, i. e.:
    Code:
    apt-get install bc
     
  12. akabuki

    akabuki New Pleskian

    6
     
    Joined:
    Apr 24, 2013
    Messages:
    2
    Likes Received:
    0
    Thanks so much :)
     
  13. Scy

    Scy Basic Pleskian

    23
    73%
    Joined:
    Feb 8, 2009
    Messages:
    67
    Likes Received:
    0
    I can also concure that this script seems to work flawlessy with Plesk 11.5.30 running under Debian 7.2 Wheezy. This script comes still handy, especially now when the new Plesk has Horde5/IMP6 webmail that no longer seems to show mailbox quota bar (at least on default settings, looking desperately a sollution for this...)
     
  14. JP Kelly

    JP Kelly Regular Pleskian

    28
     
    Joined:
    Jan 20, 2007
    Messages:
    168
    Likes Received:
    0
    I have been using this (modified) on Plesk 11.5 CentOS 5. Works great!
    Here is my version:

    Code:
    #! /bin/bash
    # Mail quota reaching it's limit e-mail notification
    # script for Plesk 9.3+ / Plesk 10 & Qmail
    # Modified by "Scy" from the original script
    # provided by "azur99" in Plesk forum:
    # [url]http://forum.parallels.com/showthread.php?t=71666[/url]
    #setenv QMAILUSER 'do-not-reply'
    MAILROOT=/var/qmail/mailnames
    cd $MAILROOT > /dev/null
    for DIR in *.*;do
            cd $MAILROOT/$DIR
            for MAILBOX in * ;do
                    if [ -d $MAILBOX ]
                            then
    
                            # look for specific mailbox quota file and set mailbox softquota
                            QUOTAFILE=$MAILROOT/$DIR/$MAILBOX/Maildir/maildirsize
    			
    			# Fetching mailbox quota size in bytes
    			HARDQUOTA=$((`head -1 $QUOTAFILE | cut -d S -f1`))
    			if [ "$HARDQUOTA" -eq 0 ]; then 
    				continue
    			fi
    		
    			# Fetching space used by mailbox in bytes
    			MBOXSPACE=$((`tail -n +2 $QUOTAFILE | cut -c1-12 | paste -sd+|bc`))
    
    			# Calculate the quota limit required for mail warning (85% for default)
    			SOFTQUOTA=$((85 * $HARDQUOTA / 100))
    
    			# Calculate mailbox usage percentage (with two decimals)
    			MBOXPERCENT=$(echo "scale=2; $MBOXSPACE*100/$HARDQUOTA" | bc)
    
    			# Check if the mailbox is full enough for warning, and if, send the warning mail
                if [ $HARDQUOTA -gt 0 -a $MBOXSPACE -gt $SOFTQUOTA ]; then
    			
    				# Let's generate the values in megabytes (with two decimals)
    				HARDQUOTA=$(echo "scale=2; $HARDQUOTA/1048576" | bc)
    				if [ "$(echo $HARDQUOTA | cut -c1)" = "." ] ; then HARDQUOTA="0"$HARDQUOTA
    				fi
    				MBOXSPACE=$(echo "scale=2; $MBOXSPACE/1048576" | bc)
    				if [ "$(echo $MBOXSPACE | cut -c1)" = "." ] ; then MBOXSPACE="0"$MBOXSPACE
    				fi
    			
    /usr/sbin/sendmail -t << EOF
    To: $MAILBOX@$DIR 
    From: ******@*******.com
    Bcc: ****@******.com
    Subject: Your mailbox is almost full
    Dear mail user,
    
    Your e-mail $MAILBOX@$DIR is about to reach its maximum quota. You are using $MBOXSPACE MB ($MBOXPERCENT%) out of the maximum quota $HARDQUOTA MB.
    
    We would kindly suggest you to delete some older messages and purge them to free some space in the mailbox. If the quota limit is reached, you won't be able to receive any new messages and the sender will receive  'mail quota exceeded' notifications.
    
    Another option is to configure your POP3 mail client (e.g. Microsoft Outlook, Mozilla Thunderbird or Apple Mail) to delete the messages on the server mailbox every time the mail account is read.
    
    This is an automated message, do not reply. If you require any assistance, please open a support ticket at [url]http://*******/support.php[/url] .
    EOF
    
    			fi
    
    
    				fi
            done;
    
    done;
     
  15. EnriqueR

    EnriqueR Regular Pleskian

    25
    23%
    Joined:
    Nov 11, 2010
    Messages:
    120
    Likes Received:
    1
    Location:
    Carcelén, 6, 3M, Albacete, Spain
    This script has been running prefectly from the first day, but now suddenly it get the following error:
    (standard_in) 1: syntax error
    Debugging I realized that comes by "bc" command to get the space. Can you help me to fix the script?
    I currently use Plesk Panel 12
     
    Last edited: Dec 3, 2014
  16. Scy

    Scy Basic Pleskian

    23
    73%
    Joined:
    Feb 8, 2009
    Messages:
    67
    Likes Received:
    0
    Are you possibly missing bc? apt-get install bc?
     
  17. EnriqueR

    EnriqueR Regular Pleskian

    25
    23%
    Joined:
    Nov 11, 2010
    Messages:
    120
    Likes Received:
    1
    Location:
    Carcelén, 6, 3M, Albacete, Spain
    Yes, it is installed correctly, but for some reason skip me this error. Anyway I've solved by replacing the line of code that fails for another:

    Line with error output:
    MBOXSPACE=$((`tail -n +2 $QUOTAFILE | cut -c1-12 | paste -sd+|bc`))

    Replacing:
    linea=0
    MBOXSPACE=0
    while read frase; do
    if [ $linea -gt 0 ]; then
    for word in $frase; do
    MBOXSPACE=$((MBOXSPACE + word))
    break
    done
    fi
    (( linea++ ))
    done < $QUOTAFILE
     
Loading...