Quick summary: Make Dovecot send a "imap notify" to your MUA when new mail arrives and make dovecot report quota usage through imap.
These are the problems I'm trying to solve:
A: When a mail arrives for a virtual user or a system user, exim uses the appendfile driver to put the mail in the user's Maildir. This driver just puts the mail there, without notifying dovecot there is new mail.
When a user has his MUA open, he will not be notified of new mail because dovecot doesn't know there is new mail. The user will see new mail when he manually clicks the "get mail" button or equivalent, triggering his MUA to explicitly ask the status of that mailbox. That triggers dovecot to scan the mailbox checking for new mail. (Note: it's possible to configure your MUA to do this automatically every 5 minutes or so).
I don't like this delay, we can do better. Instead of using exim's appendfile, I want to use dovecot's deliver, which also puts the email in the right place, but also notifies the dovecot imap process there is new mail. The MUA will instantly be notified of the new mail.
B: It's quite difficult to find out how much space you're using of your mailbox. If the admin has setup the DA squirrelmail plugin, then he can find out by logging in to squirrelmail webmail. But if you don't use or like squirrelmail you're quite of luck there. Only other option he has is to login in the DA control panel itself, but usually the end-mail-user does not have the login for the account itself, only for the mail account.
The IMAP protocol supports reporting quota usage to your MUA. That way I can see my quota usage from within my MUA and nearly all decent webmail interfaces will also report the quota usage, because they get it the same way: through IMAP.
Here is how to do it:
Step 1: configure dovecot
I only list the configuration pieces you have to modify, not the whole config.
Edit /etc/dovecot.conf:
Next create the /usr/local/bin/quota-warning.sh script which is referenced from /etc/dovecot.conf:
Don't forget to chmod +x /usr/local/bin/quota-warning.sh.
Step 2: Configure exim
Add the new dovecot transport in /etc/exim.conf:
Now you want exim to actually use this transport. Find the virtual_user director and modify it to look like this:
You can do the same for the localuser director, but I'm not using that one here.
Step 3: Configure Directadmin
Now dovecot needs to know what the quota is that you set during email account creation. Create /usr/local/directadmin/scripts/custom/create_email_post.sh.
In case you update the quota of a user, you need to rewrite the maildirsize file. Put this in /usr/local/directadmin/scripts/custom/email_change_pass_post.sh:
Don't forget to chmod +x both scripts and chown diradmin:diradmin.
What do you guys think? My customers are happier at least
I'd love to see this become the default DA setup...
Note: Suggestions from comments are constantly being incorporated in the code snippets above.
23 Apr 2010: Added mail_plugins = quota in POP3 section (yeah some people still use POP3!)
These are the problems I'm trying to solve:
A: When a mail arrives for a virtual user or a system user, exim uses the appendfile driver to put the mail in the user's Maildir. This driver just puts the mail there, without notifying dovecot there is new mail.
When a user has his MUA open, he will not be notified of new mail because dovecot doesn't know there is new mail. The user will see new mail when he manually clicks the "get mail" button or equivalent, triggering his MUA to explicitly ask the status of that mailbox. That triggers dovecot to scan the mailbox checking for new mail. (Note: it's possible to configure your MUA to do this automatically every 5 minutes or so).
I don't like this delay, we can do better. Instead of using exim's appendfile, I want to use dovecot's deliver, which also puts the email in the right place, but also notifies the dovecot imap process there is new mail. The MUA will instantly be notified of the new mail.
B: It's quite difficult to find out how much space you're using of your mailbox. If the admin has setup the DA squirrelmail plugin, then he can find out by logging in to squirrelmail webmail. But if you don't use or like squirrelmail you're quite of luck there. Only other option he has is to login in the DA control panel itself, but usually the end-mail-user does not have the login for the account itself, only for the mail account.
The IMAP protocol supports reporting quota usage to your MUA. That way I can see my quota usage from within my MUA and nearly all decent webmail interfaces will also report the quota usage, because they get it the same way: through IMAP.
Here is how to do it:
Step 1: configure dovecot
I only list the configuration pieces you have to modify, not the whole config.
Edit /etc/dovecot.conf:
Code:
protocol imap {
mail_plugins = quota imap_quota
}
protocol pop3 {
mail_plugins = quota
}
auth default {
socket listen {
master {
path = /var/run/dovecot/auth-master
mode = 0600
user = mail # User running deliver
}
}
}
protocol lda {
postmaster_address = [email protected]
hostname = yourhost.example.com
auth_socket_path = /var/run/dovecot/auth-master
mail_plugins = quota
}
plugin {
quota = maildir
quota_warning = storage=95%% /usr/local/bin/quota-warning.sh 95
quota_warning2 = storage=80%% /usr/local/bin/quota-warning.sh 80
}
Next create the /usr/local/bin/quota-warning.sh script which is referenced from /etc/dovecot.conf:
Code:
#!/bin/bash
PERCENT=$1
FROM_SMTP="[email protected]"
FROM="Support <[email protected]>"
qwf="/tmp/quota.warning.$$"
echo "From: $FROM
To: $USER
Subject: Your mailbox usage is $PERCENT% full
Content-Type: text/plain; charset="UTF-8"
Dear customer,
Your mailbox is $PERCENT% filled. Please remove some messages to
make sure new mail keeps arriving. When your mailbox is completely
filled (100%) no new mail will arrive. You can contact us at
[email protected] with your questions.
YourCompany" > $qwf
cat $qwf | /usr/sbin/sendmail -f $FROM_SMTP "$USER"
Don't forget to chmod +x /usr/local/bin/quota-warning.sh.
Step 2: Configure exim
Add the new dovecot transport in /etc/exim.conf:
Code:
dovecot_virtual_delivery:
driver = pipe
command = /usr/libexec/dovecot/deliver -d $local_part@$domain -f $sender_address -a $original_local_part@$original_domain
message_prefix =
message_suffix =
delivery_date_add
envelope_to_add
return_path_add
log_output
user = mail
temp_errors = 64 : 69 : 70: 71 : 72 : 73 : 74 : 75 : 78
Now you want exim to actually use this transport. Find the virtual_user director and modify it to look like this:
Code:
virtual_user:
driver = accept
condition = ${perl{save_virtual_user}}
domains = lsearch;/etc/virtual/domainowners
group = mail
retry_use_local_part
transport = dovecot_virtual_delivery
You can do the same for the localuser director, but I'm not using that one here.
Step 3: Configure Directadmin
Now dovecot needs to know what the quota is that you set during email account creation. Create /usr/local/directadmin/scripts/custom/create_email_post.sh.
Code:
#!/bin/bash
umask 0077
#env > /tmp/env.log
function error
{
MSG="To: [email protected]\nSubject: Error in $0\n\nError occured running $0. Error was: $1\n"
echo -en $MSG | /usr/sbin/sendmail [email protected]
exit 1
}
[[ "$quota" == "" ]] && error "quota"
[[ "$username" == "" ]] && error "username"
[[ "$domain" == "" ]] && error "domain"
[[ "$user" == "" ]] && error "user"
newquota=$((quota*1024*1024))
MAILDIR=/home/$username/imap/$domain/$user/Maildir
FILE=$MAILDIR/maildirsize
mkdir -p $MAILDIR
echo "${newquota}S,0C" > $FILE
chown -R $username:mail $MAILDIR || error "chown"
chmod 660 $FILE || error "chmod"
exit 0
In case you update the quota of a user, you need to rewrite the maildirsize file. Put this in /usr/local/directadmin/scripts/custom/email_change_pass_post.sh:
Code:
#!/bin/bash
env 0077
#env > /tmp/env.log
function error
{
MSG="To: [email protected]\nSubject: Error in $0\n\nError occured running $0. Error was: $1\n"
echo -en $MSG | /usr/sbin/sendmail [email protected]
exit 1
}
[[ "$quota" == "" ]] && error "quota"
[[ "$username" == "" ]] && error "username"
[[ "$domain" == "" ]] && error "domain"
[[ "$user" == "" ]] && error "user"
[[ "$newuser" == "" ]] && error "newuser"
newquota=$((quota*1024*1024))
MAILDIR="/home/$username/imap/$domain/$user/Maildir"
FILE="$MAILDIR/maildirsize"
NEWMAILDIR="/home/$username/imap/$domain/$newuser/Maildir"
NEWFILE="$NEWMAILDIR/maildirsize"
[[ -d "${NEWMAILDIR}" ]] || error "maildir"
echo "${newquota}S,0C" > $NEWFILE
# Force quota recalculation
echo "-1024 -1" >> $NEWFILE
chown $username:mail $NEWFILE || error "chown"
exit 0
Don't forget to chmod +x both scripts and chown diradmin:diradmin.
What do you guys think? My customers are happier at least

Note: Suggestions from comments are constantly being incorporated in the code snippets above.
23 Apr 2010: Added mail_plugins = quota in POP3 section (yeah some people still use POP3!)
Last edited: