Skip to content

Mail filter and rules

You can filter your incoming mail with Sieve. Sieve scripts can be used to automatically delete or forward mail, to send autoreplies, to sort mail into folders as they arrive, to mark mail as read or flagged or to reject mail at or after delivery.

A Sieve script consists of a number of conditions which are applied to incoming mail; if an mail message matches a test, then the actions associated with that test are performed. You can store as many Sieve scripts as you like but only one can be active at a time. There’s a good Sieve reference which describes the components which make up a script.

Access the scripts

Many mail clients support the protocol, you can find a list of tools and plugins at sieve.info. For example you can edit your scripts in the webmailer.

ManageSieve

Setting Value
Server your hostname (<something>.uberspace.de)
Port 4190
Username Your mail address, including the domain
Password Your password for the mail address

Examples

No one needs to reinvent the wheel, so we've collected a few examples for you:

Sort mail into folder

In this example we sort mail from a mailinglist mailinglist@allcolorsarebeautiful.example into a folder, sort mail to *@allcolorsarebeautiful.example into another folder and lower the maximum spam score to 4.

require ["fileinto", "reject", "relational", "comparator-i;ascii-numeric"];

# Mail with a spam score greater than 4 is probably SPAM, sort it and stop
if allof (
not header :matches "X-Rspamd-Score" "-*",
header :value "ge" :comparator "i;ascii-numeric" "X-Rspamd-Score" "4")
{
    fileinto "Spam";
    stop;
}

# Sort mail from mailinglist into folder mailinglist
if address :is "from" ["mailinglist@allcolorsarebeautiful.example", "anothermailinglist@allcolorsarebeautiful.example" ]
{
    fileinto "mailinglist";
    stop;
}

# Sort mail to *@allcolorsarebeautiful.example into a special folder
if address :is :domain "to" "allcolorsarebeautiful.example"
{
    fileinto "important";
    stop;
}

# Forward all incoming mail to multiple recipients
redirect "recipient1@example.com";
redirect "recipient2@example.com";

# The command "keep" is executed automatically, if no other action is taken.
Vacation auto reply
require ["variables", "vacation"];

if header :matches "Subject" "*" {
set "subjwas" "${1}";
}

vacation

# Reply at most once a week to a same sender
:days 7
:subject "Out of office ${subjwas}"
"I'm not in the office from 20 May until 31 May.

Best regards
Jane Doe";
# The command "keep" is executed automatically, if no other action is taken.
Send notification about new mail to another mail address
require ["enotify", "variables"];

# Set notification address
set "notify_address" "mailto:notfyme@example.com";

# Set From address for notification
set "notify_from" "mailnotifier@example.com";

# Store From address and Subject in variables
if address :matches "from" "*" {
    set "from_address" "${1}";
}
if header :matches "subject" "*" {
    set "subject" "${1}";
}

# Send notification mail
notify :from "${notify_from}" :message "New mail message received from: ${from_address} Subject: ${subject}" "${notify_address}";

Debugging

You can use the sieve-test command-line utility to test your Sieve filter script and see which options would be performed:

[isabell@moondust ~]$ ENVELOPE_SENDER=sender@example.com # replace sender@example.com with the sender address to test
[isabell@moondust ~]$ ENVELOPE_RECIPIENT=recipient@example.com # replace recipient@example.com with the recipient address to test
[isabell@moondust ~]$ TEST_MAILBOX=isa # replace isa with the name of the mailbox to test
[isabell@moondust ~]$ EMAIL_FILE=/home/isabell/testmail.eml # replace the path with the path to the .eml-file you want to test
[isabell@moondust ~]$ sieve-test -D -t - -Tlevel=matching -f $ENVELOPE_SENDER -a $ENVELOPE_RECIPIENT -l $HOME/users/$TEST_MAILBOX script.sieve $EMAIL_FILE

You can also use Fastmail’s Sieve Tester to test the syntax of scripts and checks what actions a script causes to the provided mail message.