Your mail server doesn't have infinite space. Especially if you use IMAP then users appreciate the comfort of leaving their emails on the server. And there are even IMAP users who are not aware of emails that are just marked for deletion but are still occupying space. So unless you have very few or just highly disciplined users you may want to limit the space a user can occupy. Dovecot can store the size of a user's mailbox and the number of mails therein along with the virtual folder.
There are three locations in the /etc/dovecot/dovecot.conf file where you need to enable the quota plugin (taken from the Dovecot documentation):
protocol imap {
mail_plugins = quota imap_quota
}
protocol pop3 {
mail_plugins = quota
}
protocol lda {
mail_plugins = quota
}
The simplest case is a general quota limit for all users. Say you grant every user 1 GB of space with no more than 1000 emails. This would be the configuration in your /etc/dovecot/dovecot.conf file:
plugin {
quota = maildir:storage=1000000:messages=1000
}
Keep in mind that the storage parameter refers to KB. You can even omit the messages parameter if you don't intend to limit the number of messages.
If you have certain users who have different quotas from your global quota setting then you can store the quota settings in your virtual_users table. Use this SQL statement to add two columns to the virtual_users table:
mysql>
ALTER TABLE `virtual_users` ADD `quota_kb` INT NOT NULL,
ADD `quota_messages` INT NOT NULL ;
And you will have to enable the user_query in your /etc/dovecot/dovecot-sql.conf. Throughout this tutorial it was suggested you use the "userdb static" approach in your /etc/dovecot/dovecot.conf. But this assumes that all users have equal settings. So in this per-user quota situation you will need to disable "userdb static" again and enable "userdb sql" to make Dovecot make an extra query to fetch the user's data from the database:
#userdb static {
# args = uid=5000 gid=5000 home=/var/vmail/%d/%n allow_all_users=yes
#
userdb sql {
args = /etc/dovecot/dovecot-sql.conf
}
And in your /etc/dovecot/dovecot-sql.conf add this line:
user_query = SELECT CONCAT('/var/vmail/',CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1))) AS home, 5000 AS uid, 5000 AS gid, CONCAT('maildir:storage=',quota_kb,':messages=',quota_messages) AS quota FROM virtual_users WHERE email='%u';
This query may look a bit weird but it does the same as the above
args = uid=5000 gid=5000 home=/var/vmail/%d/%n allow_all_users=yes
line and in addition gets the quota_kb and quota_messages information.
Quotas in Dovecot aren't especially user-friendly. For example the recipient does not get a warning at a soft quota limit. Just the sender gets a bounce email with the subject reading "Automatically rejected mail" saying "Your message to <john@example.com> was automatically rejected: Quota exceeded"