Email Services: Filter and Sort with Sieve

10 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Filtering email will prevent you from having an Inbox with 700 messages all clamoring for your attention. Using a tool like Sieve, you can have your work emails sorted into a Work folder, spam emails automatically deleted, and personal emails sorted into a Personal folder for you to look at when you have time. This lab will let you practice how to install and configure Sieve to filter emails.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Install and configure Sieve

Our first step is to edit:

vim /etc/dovecot/conf.d/15-lda.conf

Find the protocol lda section and ensure it looks like this:

protocol lda {
  # Space separated list of plugins to load (default is global mail_plugins).
  mail_plugins = sieve

Now configure postfix:

vim /etc/postfix/ 

Find the mailbox_command setting and set it to use the dovecot specific delivery binary:

mailbox_command = /usr/libexec/dovecot/deliver

Install Sieve:

yum install dovecot-pigeonhole -y
vim /etc/dovecot/conf.d/90-plugin.conf

Set the plugin up like so:

plugin {
  # setting_name = value
    sieve_dir = ~/sieve
    sieve_global_dir = /home/sieve
    sieve_max_script_size = 1M

Now we need to edit dovecot‘s config again:

vim /etc/dovecot/dovecot.conf

The protocols line should include the sieve plugin:

protocols = imap sieve

Edit the 20-managesieve.conf file and uncomment the following lines:

vim /etc/dovecot/conf.d/20-managesieve.conf

service managesieve-login {
  inet_listener sieve {
    port = 4190

Now we can build our Sieve script:

vim /home/test_user/.dovecot.sieve

Your script should look something like this:

require "fileinto";

if header :contains "Subject" "WORK" {
        fileinto "Work";

Restart postfix and dovecot:

systemctl restart postfix
systemctl restart dovecot

Sieve is configured!

Test your Sieve script
  1. To test your sieve script, you should send a test email.

    telnet localhost 25
    Trying ::1...
    Connected to localhost.
    Escape character is '^]'.
    220 ESMTP Postfix
    ehlo localhost
    250-SIZE 10240000
    250 DSN
    mail from:
    250 2.1.0 Ok
    rcpt to:
    250 2.1.5 Ok
    354 End data with <CR><LF>.<CR><LF>
    Subject: WORK
    250 2.0.0 Ok: queued as 3CA4197CBF
  2. Once sent, copy the message ID (the number after "queued as" above) and use it to grep the mail log:

    [root@server1 test_user]# grep 3CA4197CBF /var/log/maillog
    Jun  8 06:22:53 server1 postfix/smtpd[12092]: 3CA4197CBF: client=localhost[::1]
    Jun  8 06:22:59 server1 postfix/cleanup[12096]: 3CA4197CBF: message-id=<>
    Jun  8 06:22:59 server1 postfix/qmgr[1670]: 3CA4197CBF: from=<>, size=323, nrcpt=1 (queue active)
    Jun  8 06:22:59 server1 dovecot: lda(test_user): sieve: msgid=<>: stored mail into mailbox 'Work'
    Jun  8 06:22:59 server1 postfix/local[12097]: 3CA4197CBF: to=<>, relay=local, delay=16, delays=16/0.01/0/0.08, dsn=2.0.0, status=sent (delivered to command: /usr/libexec/dovecot/deliver)
    Jun  8 06:22:59 server1 postfix/qmgr[1670]: 3CA4197CBF: removed

    As you can see, the 4th line indicates that Sieve sorted the email into the correct folder. Congratulations!

Additional Resources

You've been given a pre-configured dovecot server. Install and configure sieve and make sure it can filter emails to test_user that have the word "WORK" in the subject into the Work IMAP folder.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?