Setting up Email Filtering and Autoresponder Rules using Sieve Scripts in Dovecot

0 0
Read Time:4 Minute, 55 Second

Introduction

In a previous blog post, we showed you how to set up a mail server on Linux using Postfix, Dovecot, Roundcube, ClamAV and Amavis. In this tutorial, we’ll build on that setup by showing you how to use Sieve scripts in Dovecot to filter incoming email messages and set up autoresponder rules.

What is Sieve?

Sieve is a scripting language used to filter and manipulate email messages. Sieve scripts can be used to automatically sort messages into folders, forward or delete messages based on specific criteria, and even send automated replies. Dovecot, the mail server software we used in our previous tutorial, supports Sieve scripts.

Installing and Configuring Dovecot’s Sieve Plugin

To use Sieve scripts in Dovecot, we first need to install and configure the Sieve plugin. On Ubuntu and other Debian-based systems, you can install the plugin using the following command:

sudo apt-get install dovecot-sieve dovecot-managesieved

Next, we need to enable the plugin in the Dovecot configuration file. Open the file /etc/dovecot/conf.d/20-lmtp.conf and add the following lines to enable the plugin:

protocol lmtp {
  mail_plugins = $mail_plugins sieve
}

Save the file and restart Dovecot:

sudo service dovecot restart

Creating and Managing Sieve Scripts

Now that we have the Sieve plugin installed and configured, we can start creating and managing Sieve scripts. Sieve scripts are text files that can be edited with any text editor, or using Roundcube’s web-based editor.

Using a Text Editor

To create a Sieve script using a text editor, follow these steps:

  1. Create a new file with a .sieve extension, such as filter.sieve.
  2. Add the following code to the file to create a basic filtering rule that moves messages from a specific sender to a folder called “Mailing Lists”:
require ["fileinto"];

if header :contains "From" "[email protected]" {
    fileinto "Mailing Lists";
}
  1. Save the file and move it to the appropriate directory. On Ubuntu and other Debian-based systems, the directory is /etc/dovecot/sieve. You’ll also need to give the file the correct permissions:
sudo mv filter.sieve /etc/dovecot/sieve
sudo chown root:root /etc/dovecot/sieve/filter.sieve
sudo chmod 644 /etc/dovecot/sieve/filter.sieve
  1. Finally, you’ll need to tell Dovecot to use this script for your mailbox. Open your Dovecot configuration file (/etc/dovecot/conf.d/90-sieve.conf) and add the following line to the bottom of the file:
plugin {
  sieve = /etc/dovecot/sieve/filter.sieve
}

Save the file and restart Dovecot:

sudo service dovecot restart

Using Roundcube’s Web-based Editor

Alternatively, you can use Roundcube’s web-based editor to create and manage Sieve scripts. To do this, follow these steps:

  1. Log in to Roundcube and click on the “Settings” menu.
  2. Click on the “Filters” tab.
  3. Click the “New filter” button to create a new filter rule.
  4. Use the dropdown menus to specify the conditions for the filter rule, such as the sender or recipient of the message.
  5. Use the “Action” dropdown menu to specify what should happen to messages that match the filter criteria, such as moving them to a folder or deleting them.
  6. Click the “Save” button to save the filter rule.

Roundcube will automatically generate the necessary Sieve code and save it to the appropriate directory.

Setting up Autoresponder Rules

In addition to filtering email messages, Sieve scripts can also be used to set up autoresponder rules. Autoresponders are useful for automatically replying to messages when you’re out of the office, on vacation, or otherwise unavailable.

To set up an autoresponder rule, follow these steps:

  1. Create a new Sieve script, as described in the previous section.
  2. Add the following code to the script to create a basic autoresponder rule:
require ["vacation"];

vacation
  :subject "Out of Office"
  :message "I'm currently out of the office and will not be checking my email. I'll respond to your message when I return."
  :days 7
  :addresses ["[email protected]"]
;

This code will send an autoresponder message with the subject “Out of Office” and the specified message body to anyone who sends a message to the address [email protected]. The autoresponder will be active for 7 days.

  1. Save the file and move it to the appropriate directory, as described in the previous section.
  2. Restart Dovecot to activate the new Sieve script.

Conclusion

Using Sieve scripts in Dovecot is a powerful way to filter and manage email messages. With Sieve, you can automatically sort messages into folders, forward or delete messages based on specific criteria, and even send automated replies. By following the steps in this tutorial, you should now be able to create and manage your own Sieve scripts in Dovecot, and set up autoresponder rules for when you’re out of the office.

Remember that Dovecot’s Sieve plugin is just one part of a complete mail server setup. If you haven’t yet set up a mail server using Postfix, Dovecot, Roundcube, ClamAV and Amavis, be sure to check out our previous tutorial on “How to Set Up a Mail Server on Linux with Postfix, Dovecot, Roundcube, ClamAV and Amavis” for a complete guide.

For some further reading, check out the links below;

  1. The Dovecot Wiki – the official wiki for Dovecot, which provides documentation and resources for using Dovecot’s features, including Sieve.
  2. The Sieve Language Specification – the official RFC for the Sieve language, which provides a comprehensive reference for the language’s syntax and features.
  3. The Roundcube Webmail Project – the official website for the Roundcube webmail client, which provides a web-based interface for creating and managing Sieve scripts.
  4. Spamhaus – a non-profit organization that provides anti-spam and anti-cybercrime services, including lists of known spammers and malware sources.
  5. The ClamAV Project – the official website for ClamAV, an open-source antivirus engine that can be integrated with Dovecot to scan email messages for malware.

We hope this tutorial has been helpful. If you have any questions or feedback, feel free to leave a comment below.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “Setting up Email Filtering and Autoresponder Rules using Sieve Scripts in Dovecot

Leave a comment