Dovecot has built-in support for the Sieve mail filtering language. It is very useful to do the server-side email processing such as:
- Removing unwanted e-mail messages before they are delivered to your inbox;
- Copying or moving e-mail messages to different folders;
- Creating vacation autoresponses or any other kind of autoresponses
- Configuring the actions above depending on message sender, recipient, subject, body, and so on.
This useful functionality is provided by the Dovecot mail delivery agent, dovecot-lda. However since we do not use it for delivery, it is not enabled. So the first step would be to enable it.
Do you have the Sieve plugin?
The Sieve language support plugin is not part of Dovecot source code. It is provided as a separate source which may or may not be packaged by your Linux distro. OpenSUSE packagers did the great job and dovecot bundled with OpenSUSE contains both Sieve and ManageSieve extensions. You may be less lucky in which case you may have to build it yourself.
An easy way to check if you have it is to see whether you have the configuration file /etc/dovecot/conf.d/90-sieve.conf in your dovecot installation. This file only comes with the Sieve plugin, so if you have this file, this means you have the plugin as well.
Enabling the Sieve plugin
The Sieve plugin could be enabled in the Dovecot configuration by editing the /etc/dovecot/conf.d/15-lda.conf file and adding sieve into the list of those mail plugins loaded by default:
protocol lda { # Space separated list of plugins to load (default is global mail_plugins). mail_plugins = $mail_plugins sieve }
Enabling the dovecot-lda authentication agent
Unlike the getmail, the dovecot-lda does not know the Maildir path where to deliver the mail. Instead it receives the virtual user name as the command-line parameter, and queries the path from the dovecot authentication process. This makes the configuration simpler, but this also means the authentication should be enabled in the dovecot configuration. This is done by the auth-userdb service specified in the /etc/dovecot/conf.d/10-master.conf file and it is enabled by default. However in our case it needs a little tweaking since we run getmail under our mailman user and it will start the dovecot-lda process under the same user. By default this user does not have access to the dovecot authentication service and will not be able to query the necessary information. Therefore we need to change its permission by editing the auth-userdb section and changing the user the socket permissions as following:
unix_listener auth-userdb { mode = 0600 user = mailman #group = }
Then restart dovecot. The dovecot-lda can now be used.
Reconfiguring getmail to use dovecot-lda for mail delivery
To use the dovecot-lda for mail delivery getmail needs to be reconfigured as it needs to be explicitly told to use the mail delivery agent instead of doing direct delivery. This is done by modifying the [destination] section in the getmailrc configuration file. The new section should look like the one below:
[destination] type = MDA_external path = /usr/lib/dovecot/dovecot-lda arguments = ("-f", "%(sender)", "-d", "pers" )
As you see the path to the destination mailbox is now specified anymore. Instead the -d command-line option followed up by the virtual user name (which in our case is pers) is used to tell the dovecot-lda which user this email is for. The mail directory is looked up through the dovecot authentication agent.
Now send yourself the test e-mail and verify that it gets properly delivered. Check the /var/log/mail in case of any errors, which will likely be triggered by either the wrong virtual username or lack of permissions to access the dovecot authentication process socket.
Managing the Sieve scripts from your e-mail client
Bundled together with Sieve, Dovecot provides a nice way to manage the Sieve scripts right from your e-mail client assuming it has necessary capabilities. KMail can do it natively, Thunderbird can do it with the Sieve add-on.
This functionality must be enabled in Dovecot to be supported, but it is fairly easy to enable. Just edit the /etc/dovecot/conf.d/20-managesieve.conf file and uncomment the line started with protocols:
# Uncomment to enable managesieve protocol: protocols = $protocols sieve
Then restart dovecot. That’s all.
Sample Sieve script
An example of Sieve script which does various tasks:
require ["fileinto", ["vacation"];
if header :is "From" "john.doe@high.profile.business.com" { # Store business emails from john.doe in a Work/JohnCompany folder fileinto "Work.JohnCompany"; } elsif header :is "From" "buyviagra@spam.com" { # Just delete that spam discard; stop; } elsif header :contains "From" "john.doe@" { # If this is email from john.doe sent from any other of his accounts, move it to Parties folder fileinto "Parties"; } else { # Tell everyone else I'm on vacation vacation # Reply at most once a day to a same sender :days 1 # The auto-reply subject :subject "Out of country: vacation" # The reply text "I will be out of country for a week. Please call my mobile +X XXX XXXXXXXXXX for urgent matters.
Best regards John Smith"; }