With a existing Postfix and Dovecot configuration on a Ubuntu 20.04 server, I required the following changes to get sieve server based mail filtering to work:
Install dovecot-sieve
To install the dovecot sieve plugin run
apt install dovecot-sieve
Change the mailbox transport to lmtp
Edit /etc/postfix/main.cf to set the line
mailbox_transport = lmtp:unix:private/dovecot-lmtp
If dovecot is configured to use local usernames (without @domain on the end), you also need to edit /etc/dovecot/conf.d/10-auth.conf to add this line:
auth_username_format = %Ln
This means that for the username dovecot will use the lowercase part to the left of the "@".
Restart daemons
Restart postfix and dovecot so they use the new config, and then check their status:
systemctl restart postfix dovecot
systemctl status postfix dovecot
Enable the sieve plugin in dovecot
Add this section to /etc/dovecot/conf.d/20-lmtp.conf:
protocol lmtp {
mail_plugins = $mail_plugins sieve
}
Configure a user's sieve filters
The default location for a user's sieve filters is configured in /etc/dovecot/conf.d/90-sieve.conf as follows:
plugin {
sieve = file:~/sieve;active=~/.dovecot.sieve
}
So, as yourself (not root) run:
cd
mkdir sieve
touch default.sieve
ln -s sieve/default.sieve .dovecot.sieve
Then edit ~/sieve/default.sieve with your sieve configuration, for example:
require ["fileinto"};
if header :contains "Subject" "test" {
fileinto "Test";
}
For full details about sieve filters see see RFC5228 Sieve: An Email Filtering Language.
Test
Send yourself an email with "test" (lowercase) in the subject. It should end up in the Test mail folder.
If the test mail isn't filtered check ~/.dovecot.sieve.log and /var/log/mail.log for problems.