Filter Emails on the Server with dovecot-sieve

Based on the follwing thread in our Forum I wanted to give dovecot-sieve a try Thanks a lot @ Purzel :-D ;-)

Purzel wrote his post for the firmware which did not have the mailstation included then. I just put all the steps together which are still needed with actual firmwares (which already contain mailstation package)

What's that?

With dovecot-sieve it's possible to filter mails directly on the server. With that is possible to move certain mails into specified folders on the server direclty upon receive (for example to move spam-marked mails). The sieve code is running on the dovecot server (IMAP/POP3). As dovecot (ex deliver) is a LDS (Local Delivery Agent) it is possible to advise postfix (SMTP) to deliver the mails via deliver as well. With that postfix mails are in the filter proccess as well.
To achieve this it's necessary to compile the dovecot LDA (deliver) from sources of dovecot.


As base for this steps the same applies as for ipkg installations although we do not install a particlar ipkg package

  • ipkg must be present for your processor
  • access to the CLI as root via telnet or ssh
  • text editor with correct line-endings for Linux (\n AND NOT \r\n): for example vi or nano

Furthermore a C-compiler is requested on the DS, as well as the development tools and the database libraries

$ ipkg update
$ ipkg install gcc
$ ipkg install optware-devel
$ ipkg install libdb

If package gcc does not exist, you can try to install the compiler via cross-native (if it exists). If there is no native compiler for your DS then it's unfortunately not possible to install the dovecot-sieve! Furthermore the source codes for dovecot and dovecot-sieve are neeeded. It is important that you check the exact version as Purzel used in his post in the forum.

$ cd /volume1/public
$ wget
$ wget
$ gzip -d ./dovecot-1.0.15.tar.gz
$ tar xfv ./dovecot-1.0.15.tar
$ gzip -d ./dovecot-sieve-1.0.3.tar.gz
$ tar xfv ./dovecot-sieve-1.0.3.tar


Dovecot as well as dovecot-sieve need to be configured.
It is very important that you perform a configure and a make BUT NOT a make install!!!

If you encounter troubles with compiling the following post from goetz in our forum could be helpful

Compile dovecot

dovecot-sieve expects the complete source code of dovecot to be present. Therefore perform the following to prepare dovecot source (IMPORTANT: DO NOT perform make install )

$ cd /volume1/public/dovecot-1.0.15
$ ./configure && make

The code above will prepare the dovecot code so dovecot-sieve can work with it.

Install dovecot-sieve

After preparing dovecot the next step is to install dovecot-sieve. Do NOT make install for dovecot-sieve as well

$ cd /volume1/public/dovecot-sieve-1.0.3
$ ./configure --with-dovecot=../dovecot-1.0.15
$ make

This code creates the necessary modules which needed to be copied in the correct directories

Copy the modules

The first 4 files reside in src/.libs of the dovecot-sieve parent directory. The target for those need to be created in first place.

$ mkdir -p /lib/dovecot/lda

After that you can copy the modules as follows:  -> /lib/dovecot/
lib90_cmusieve_plugin.lai -> /lib/dovecot/lda/
lib90_cmusieve_plugin.a   -> /lib/dovecot/lda/  -> /lib/dovecot/lda/  -> /lib/dovecot/lda/

The following two files are in src/libsieve of dovecot parent directory. Their target directories already exist and therefore they do not need to be created first

sievec                    -> /usr/syno/mailstation/libexec/dovecot/
sieved                    -> /usr/syno/mailstation/libexec/dovecot/

Compile LDA

The „job“ of the LDA is to accept mails from other processes and fill them into the defined mailboxes. The dovecot-sieve filters will be applied form deliver (note: here you need to make install)

$ cd /volume1/public/dovecot-1.0.15/src/deliver
$ make install deliver

Now we have a file deliver in the actual directory which we need to copy at the correct place

$ cp ./deliver /usr/syno/mailstation/libexec/dovecot 

Now deliver expect the config file to be in /usr/local/etc. That can be easily solved by adding a symlink

$ ln -s /usr/syno/mailstation/etc/dovecot.conf /usr/local/etc/dovecot.conf



Now dovecot needs to know, that it should use the filter plugins. To achieve this open the configfile /usr/syno/mailstation/etc/dovecot.conf and look for the String protocol lda

protocol lda {
  # Address to use when sending rejection mails.
  postmaster_address = ich@du.tld

  # Hostname to use in various parts of sent mails, eg. in Message-Id.
  # Default is the system's real hostname.
  hostname = ich.du.tld

  # Support for dynamically loadable plugins. mail_plugins is a space separated
  # list of plugins to load.
   mail_plugins = cmusieve
   mail_plugin_dir = /usr/lib/dovecot/lda

  # If user is over quota, return with temporary failure instead of
  # bouncing the mail.
  #quota_full_tempfail = no

  # Format to use for logging mail deliveries. You can use variables:
  #  %$ - Delivery status message (e.g. "saved to INBOX")
  #  %m - Message-ID
  #  %s - Subject
  #  %f - From address
  #deliver_log_format = msgid=%m: %$

  # Binary to use for sending mails.
  #sendmail_path = /usr/lib/sendmail

  # Human readable error message for rejection mails. Use can use variables:
  #  %n = CRLF, %r = reason, %s = subject, %t = recipient
  #rejection_reason = Your message to <%t> was automatically rejected:%n%r

  # UNIX socket path to master authentication server to find users.
  #auth_socket_path = /var/run/dovecot/auth-master

Take care that you remove the comment sign ('#') before protocol lda and the one before the closing bracket ('}'). Otherwise dovecot will crash. Afterwards save the file.

Integration in getmail

To involve the LDA (and it's filter rules) when mails are loaded from external accounts (ex with getmail), you'll have to amend your rc-files accordingly. Normally getmail writes mails directly into the given mailboxes, so we have to „tell“ getmail to involve the LDA

type = MDA_external
path = /usr/syno/mailstation/libexec/dovecot/deliver
arguments = ("-e", )

That works fine for local users. But if you want to have virtual users have a look at the following discussion

Integration in Postfix

To let postfix know that he has to use the LDA as well instead of delivering mails directly to the mailboxes the following configuration in /usr/syno/mailstation/etc/ needs to be set

mailbox_command = /usr/syno/mailstation/libexec/dovecot/deliver

Save the file and restart postfix

Create a filter file

To execute actions upon receive of mails every user need the following file directly in their homedir .dovecot.sieve. This file defines the regular expressions and the actions if a match appears. In the following first a way to sort our messages marked as spam into a defined spamfolder

require "fileinto";
if header :comparator "i;ascii-casemap" :contains "subject" "*****SPAM*****" {
  fileinto "Trash";
} elsif header :comparator "i;ascii-casemap" :contains "from" "" {
   fileinto "INBOX.Computer &- Security.Foren.Synology";
} else {

As you can see you can use if/elsif/else constructs. Important note: All the expressions should be in one if/elsif/else construct. Otherwise you end up having a mail in the inbox and in the defined folder as well
You can „connect“ several expressions into one check. First shown with the OR operator

if anyof(header :comparator "i;ascii-casemap" :contains "from" "",header :comparator "i;ascii-casemap" :contains "subject" "Your Support Request") {
   fileinto "INBOX.Computer &- Security.Foren.Synology";

And now with the AND operator

if allof(header :comparator "i;ascii-casemap" :contains "from" "",header :comparator "i;ascii-casemap" :contains "subject" "Your Support Request") {
   fileinto "INBOX.Computer &- Security.Foren.Synology";
Melden Sie sich an, um einen Kommentar zu erstellen.