Today we will be talking about how to configure user and domain quotas with dovecot 2 on virtual users mail service.
At this time we are assuming you have a fully running mail server and you just want to implement domain and mailbox quotas to control individual users storage limit.

This tutorial will use the following softwares to reach the goal:-

Dovecot 2.2   for handling the quota measures on the users mailboxes
PostfixAdmin for setting the mailbox and domain quota limits

The text highlgted in red are the files you need to edit, and the text highlited in green are the changes you need to add on the edited files

First of all, we will edit some settings on postfixadmin config file to enable the quota table on editing domain and mailboxes

Edit the file with your favorite editor  /etc/postfixadmin/config.inc.php look for the lines below and edit them accordingly

// When you want to enforce quota for your mailbox users set this to 'YES'.
$CONF['quota'] = 'YES';

// Optional:
// Show used quotas from Dovecot dictionary backend in virtual
// mailbox listing.
// See: DOCUMENTATION/DOVECOT.txt
//      http://wiki.dovecot.org/Quota/Dict
//
$CONF['used_quotas'] = 'YES';

// if you use dovecot >= 1.2, set this to yes.
// Note about dovecot config: table "quota" is for 1.0 & 1.1, table "quota2" is for dovecot 1.2 and newer
$CONF['new_quota_table'] = 'YES';

After finished editing the three lines, save the changes and restart web server for the changes to take effect

# /etc/init.d/apache2 restart

You can try to browse postfixadmin and login to see if the changes are available on adding domain or mailbox interface

Now lets move to Dovecot and do the really quota job that will handle the mailbox size and give notifications incase the mailbox is full or amost
We will create two files that will handle users and domain quotas
You can use the command below  to create  and add the content below in the file

# vim /etc/dovecot/dovecot-dict-sql-user.conf 

connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password=postfixadmin
map {
pattern = priv/quota/storage
table = quota2
username_field = username
value_field = bytes
}
map {
pattern = priv/quota/messages
table = quota2
username_field = username
value_field = messages
}

# vim /etc/dovecot/dovecot-dict-sql-domain.conf

connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password=postfixadmin
map {
pattern = priv/quota/storage
table = domain
username_field = domain
value_field = quota
}
map {
pattern = priv/quota/messages
table = quota2
username_field = username
value_field = messages
}

Edit the below files to make some changes

# vim /etc/dovecot/conf.d/auth-sql.conf.ext

userdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf.ext
}

# vim /etc/dovecot/conf.d/10-master.cf

service dict {
unix_listener dict {
mode = 0600
user = vmail
}
}

# vim /etc/dovecot/conf.d/10-mail.conf

mail_plugins = $mail_plugins quota

# vim /etc/dovecot/conf.d/20-imap.conf

mail_plugins = $mail_plugins imap_quota

You can also edit /etc/dovecot/conf.d/20-pop3.conf and set mail_plugins = $mail_plugins pop3_quota if you use pop for your customers, but its not really necessary for now

Now Edit  vim /etc/dovecot/conf.d/90-quota.conf and add the following lines at the end

plugin {
# Using SQL Tables to store current quota size
quota = dict:Quota:%d:proxy::sqldomainquota
quota = dict:User Quota::proxy::sqluserquota
# Allow 10% more for Trash Folder
quota_rule2 = Trash:storage=+10%%
}
dict {
# sqlquota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext
sqluserquota = mysql:/etc/dovecot/dovecot-dict-sql-user.conf
sqldomainquota = mysql:/etc/dovecot/dovecot-sql-domain.conf
}

Now as we are approaching the end of this tutorial, lets edit the file that connects to the MySQL Database to check the users and passwords for authentications.
We will modify the queries to check user and domain quotas set before a mail can be delivered

password_query = SELECT password, concat('Maildir:/var/vmail/',maildir,'/') AS userdb_home, 500 AS userdb_uid, 500 AS userdb_gid FROM mailbox WHERE username = '%Lu' AND active = '1';

user_query = SELECT maildir, concat('/var/vmail/',maildir) AS home, 500 AS uid, 500 AS gid, concat('*:storage=',quota,'B') AS quota_rule, 'Trash:storage=+10M' AS quota_rule2, '%Lu' AS master_user FROM mailbox where username = '%Lu' AND active = '1';

Save the file and restart dovecot service for the changes to take effect
# /etc/init.d/dovecot restart

For now that’s all what we need for the creation and editing of files on postfixadmin and dovecot, Try to login if your using RoundCube webmail and check at the bottom left, it will show you the percentage of your mailbox