Mail server_Serverside filtering - SomethingWithHorizons/mailserver GitHub Wiki

Filter spam (to the user-accessible Junk folder) server sided.

Spam is a fact of life when it comes to e-mail. Without filtering it, spam could flood the users inboxes with unsolicited e-mails and obfuscate important e-mails, potentially to a level that it would even render the mailserver useless.

Setting the following server sided settings offers users with a default first line of defense against having their inboxes filled up with unwanted e-mails, based on sane spam identifying criteria. The most obvious spam e-mails (having their header marked with the X-Spam-Status: Yes by the spamfilter AMaViS) will be filtered to the user accessible Junk folder.

:information_source: This default server behavior can be overruled by users using their client of choice.

Procedure

  1. Edit /etc/dovecot/conf.d/20-lmtp.conf to enable the use of plugins and add 'sieve' (the plugin to provide the actual separation of the email files and have them stored to their designated directories) to the list of plugins to use:

      protocol lmtp {
        # Space separated list of plugins to load (default is global mail_plugins).
    -   #mail_plugins = $mail_plugins
    +   mail_plugins = $mail_plugins sieve
      }
    
  2. Edit /etc/dovecot/conf.d/90-sieve.conf to define the path to the directory that will contain the rule set that is to be run "after" other rules (e.g. as user-specific ones):

      # Identical to sieve_before, only the specified scripts are executed after the
      # user's script (only when keep is still in effect!). Multiple script
      # locations can be specified by appending an increasing number.
      #sieve_after =
      #sieve_after2 =
      #sieve_after2 = (etc...)
    + sieve_after = /etc/dovecot/sieve-after
    
  3. Create sieve-after directory correspondingly:

    mkdir /etc/dovecot/sieve-after
    
  4. Create /etc/dovecot/sieve-after/spam-to-folder.sieve and populate it with the rule to filter mails that have their header marked with the X-Spam-Status: Yes to the Junk folder:

    + require ["fileinto","mailbox"];
    
    + if header :contains ["X-Spam-Status"] "Yes" {
    +   fileinto :create "Junk";
    +   stop;
    + }
    
  5. Convert the created, human readable, .sieve file to an .svbin format that sieve "understands":

    sievec /etc/dovecot/sieve-after/spam-to-folder.sieve
    
  6. Restart Dovecot service to effectuate the changes:

    service dovecot reload