Dovecot - Pigeonhole Sieve Mail Filter

This article will detail how to install Dovecot Pigeonhole sieve mail filter, and configure Postfix to hand off delivery to Dovecot using LMTP. Dovecot will run these emails through the sieve filter before final delivery to the user's mailbox.

The sieve filter allows you to create filter rules based on any number of criteria such as sender email address from the envelope, or keyword search in the subject or body of the email. Once a match is found the filter can then perform actions such as moving the email to another mail folder, deleting the email, or flagging it as Spam or Seen (read).

This article assumes that you already have fully operational Postfix and Dovecot services running on the same server. This following configuration is confirmed working with Postfix running as detailed in Postfix - SMTP Email Server, and Dovecot as detailed in Dovecot - IMAP Email Server.

 

Install Dovecot Pigeonhole

Install Dovecot Pigeonhole using your preferred method.

pkg install mail/dovecot-pigeonhole

 

Configure Dovecot

Copy the example 90-sieve.conf file to Dovecot's conf.d/ directory

cp  /usr/local/share/doc/dovecot/example-config/conf.d/90-sieve.conf /usr/local/etc/dovecot/conf.d/

 

Edit /usr/local/etc/dovecot/dovecot.conf

protocols = imap lmtp

 

Edit /usr/local/etc/dovecot/conf.d/10-auth.conf

auth_username_format = %Ln

 

Edit /usr/local/etc/dovecot/conf.d/10-master.conf

service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    group = postfix
    mode = 0600
    user = postfix
  }
}

 

Edit /usr/local/etc/dovecot/conf.d/20-lmtp.conf

protocol lmtp {
  mail_plugins = $mail_plugins sieve
  mail_fsync = optimized
}

lmtp_save_to_detail_mailbox = yes

 

Configure Postfix

Edit /usr/local/etc/postfix/main.cf

mailbox_transport = lmtp:unix:private/dovecot-lmtp

 

Restart Mail Services

Restart Dovecot and Postfix

service dovecot restart; service postfix restart

 

Create a Sieve Filter

Create a sieve .dovecot.sieve file in the desired user's home directory /home/user/.dovecot.sieve

The following provides examples of two rules:

1) Any email that contains "test filter" (not case sensitive) in the body will be flagged as seen (read).
2) Any email that has the word "casino" in the subject will be moved to the Spam mailbox folder.

require ["fileinto", "envelope", "body", "imap4flags"];

if body :contains "test filter" {
  addflag "\\Seen";
  stop;
}

if header :contains "Subject" "casino" { fileinto "Spam"; stop; }

 

Test the Configuration

Send test emails to yourself to verify that emails are being delivered via lmtp and that the filter ruleset is working

tail -f /var/log/maillog watching for dovecot: lmtp messages as you send test emails to yourself.