This documentation is for Dovecot v2.x, see wiki1 for v1.x documentation.

Replacing antispam plugin with IMAPSieve

You will need at least pigeonhole v0.4.14 for this. If you have already configured sieve, please adjust the following to match your setup.

Caveats and possible pitfalls


Dovecot configuration

protocol imap {
  mail_plugins = $mail_plugins imap_sieve

plugin {
  sieve_plugins = sieve_imapsieve sieve_extprograms

  # From elsewhere to Spam folder
  imapsieve_mailbox1_name = Spam
  imapsieve_mailbox1_causes = COPY
  imapsieve_mailbox1_before = file:/usr/lib/dovecot/sieve/report-spam.sieve

  # From Spam folder to elsewhere
  imapsieve_mailbox2_name = *
  imapsieve_mailbox2_from = Spam
  imapsieve_mailbox2_causes = COPY
  imapsieve_mailbox2_before = file:/usr/lib/dovecot/sieve/report-ham.sieve

  sieve_pipe_bin_dir = /usr/lib/dovecot/sieve

  sieve_global_extensions = +vnd.dovecot.pipe +vnd.dovecot.environment

Sieve scripts

Create directory /usr/lib/dovecot/sieve and put following files to that:


require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];

if environment :matches "imap.user" "*" {
  set "username" "${1}";

pipe :copy "" [ "${username}" ];


require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"];

if environment :matches "imap.mailbox" "*" {
  set "mailbox" "${1}";

if string "${mailbox}" "Trash" {

if environment :matches "imap.user" "*" {
  set "username" "${1}";

pipe :copy "" [ "${username}" ];

Shell scripts

For spamassassin

exec /usr/bin/sa-learn -u ${1} --spam

exec /usr/bin/sa-learn -u ${1} --ham

For dspam

exec /usr/bin/dspam --client --user ${1} --class=spam --source=error

exec /usr/bin/dspam --client --user ${1} --class=innocent --source=error

CRLF handling

dspam may fail to read the mail if it contains CRLF line endings, add the Broken lineStripping option in dspam.conf if needed.

For rspamd

By default, rspamd does global learning. If you want per-user classification, or something more complex, see

exec /usr/bin/rspamc -h /run/rspamd/worker-controller.socket -P <secret> learn_spam

exec /usr/bin/rspamc -h /run/rspamd/worker-controller.socket -P <secret> learn_ham

Before running following commands, make sure dovecot.conf has all the sieve configuration you want. Then run following commands:

sievec /usr/lib/dovecot/sieve/report-spam.sieve
sievec /usr/lib/dovecot/sieve/report-ham.sieve
chmod +x /usr/lib/dovecot/sieve/ /usr/lib/dovecot/sieve/

Now your learn scripts should be invoked when you move mails between folders.


To debug, you need to import "vnd.dovecot.debug" extension. Then you can put, when required

debug_log "something"

variables are supported in this.


Recent versions of RoundCube include a plugin (located in plugins/markasjunk) for allowing users to mark Spam/Ham in a convenient way. Please make sure the Junk/Spam folder matches your configuration.

HowTo/AntispamWithSieve (last edited 2017-10-18 07:57:52 by HSI-KBW-46-237-197-153)