Mail quota

From VHCP Support

Jump to: navigation, search

This is one solution to add quota to postfix mail accounts. There isn't any GUI to manage quotas, that needs to be done from mysql console. By default, every user has 10 megabytes quota.

Preparing Postfix + VDA packages

apt-get install build-essential dpkg-dev fakeroot
apt-get build-dep postfix
apt-get source postfix
cd /usr/src
wget http://web.onda.com.br/nadal/postfix/VDA/postfix-2.2.4-vda.patch.gz
gunzip postfix-2.2.4-vda.patch.gz
cd postfix-2.2.4
patch -p1 < ../postfix-2.2.4-vda.patch
dpkg-buildpackage (gives error about some packages missing, install those..)
cd ..

dpkg -i postfix_2.2.4-1_i386.deb
dpkg -i postfix-mysql_2.2.4-1_i386.deb

Editing mail_users table to add default 10 megabytes quota. (10485760 bytes = 10 megabytes)

mysql -u root -pPASSWORD
USE vhcp;
ALTER TABLE `mail_users` ADD `quota` INT(10) DEFAULT '10485760', ADD `mail_addr` VARCHAR(200);

Create file /etc/postfix/mysql_virtual_mailbox_limit_maps.cf with following content: (edit user, password, hostname and dbname to match right ones..)

user = vhcp
password = vhcp
hosts = localhost
dbname = vhcp
table = mail_users
select_field = quota
where_field = mail_addr

Add following lines to /etc/postfix/main.cf

virtual_create_maildirsize = yes
virtual_mailbox_extended = yes
virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf
virtual_mailbox_limit_override = yes
virtual_maildir_limit_message = "The user you're trying to reach is over quota."
virtual_overquota_bounce = yes

Configure mail user adding page /var/www/webadmin/gui/client/add_mail_acc.php

line 220, add after $sub_id = '0';

$mail_addr = $mail_acc.'@'.$dmn_name;

line 226, add after $sub_id = $_POST['sub_id'];

$query = <<<SQL_QUERY
	select
		subdomain_name
	from
		subdomain
	where
		subdomain_id = ?
SQL_QUERY;

$rs = exec_query($sql,$query,array($sub_id));
$mail_addr = $mail_acc.'@'.decode_idna($rs->fields['subdomain_name']).'.'.$dmn_name;

line 240, add after sub_id = $_POST['als_id'];

$query = <<<SQL_QUERY
	select 
		alias_name
	from
		domain_aliasses
	where
		alias_id = ?
SQL_QUERY;

$rs = exec_query($sql,$query,array($sub_id));
$mail_addr = $mail_acc.'@'.decode_idna($rs->fields['alias_name']);

line 352, change this:

        insert into mail_users
            (mail_acc,
             mail_pass,
             mail_forward,
             domain_id,
             mail_type,
             sub_id,
             status,
             mail_auto_respond)
        values
            (?, ?, ?, ?, ?, ?, ?, ?)

to this:

        insert into mail_users
            (mail_acc,
             mail_pass,
             mail_forward,
             domain_id,
             mail_type,
             sub_id,
             status,
             mail_auto_respond,
             mail_addr)
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?)

Change this:

  $rs = exec_query($sql, $query, array($mail_acc,
                                       $mail_pass,
                                       $mail_forward,
                                       $domain_id,
                                       $mail_type,
                                       $sub_id,
                                       $status,
                                       $mail_auto_respond));

to this:

  $rs = exec_query($sql, $query, array($mail_acc,
                                       $mail_pass,
                                       $mail_forward,
                                       $domain_id,
                                       $mail_type,
                                       $sub_id,
                                       $status,
                                       $mail_auto_respond,
				       $mail_addr));

Already configured add_mail_acc.php can be found here: http://puuhis.net/vhcs/add_mail_acc (works with 2.4.6.2)

Restarting postfix

/etc/init.d/postfix restart

Notice that quota is working now only to new mail accounts, because old accounts doesn't have mail_addr field in mail_users table.. It's added there when creating a new mail user. So you have to add those missing mail_addr fields there manually.

Quota notifier

But, what good is a Postfix with quotas if customer doesn't know he/she is over quota? Yes, we need a quota notifier.. There exists some perl script which does this job, thanks to Jean-Paul Stewart for this!

http://puuhis.net/vhcs/quota.txt

Just configure those variables on top of script and add to cronjob to run daily for example..

External links

Personal tools