Sieve is a mail filtering language used by email servers to automatically process incoming messages. It allows users to create rules that sort, move, forward, reject, or delete emails based on specific conditions.
By using Sieve filters, you can automate mailbox management and reduce the time spent organizing messages manually.
Table of Contents
Basic Elements of a Sieve Script
Comments
Comments are ignored by the Sieve interpreter and are useful for documenting your rules.
# This is a comment
Required Extensions
Most Sieve scripts begin by declaring the extensions that will be used.
require ["fileinto", "reject"];
These extensions enable additional actions such as moving messages to folders or rejecting messages.
Conditions
Conditions determine when a specific action should be performed.
if header :contains "Subject" "important" {
# actions
}
In this example, the rule checks whether the email subject contains the word “important”.
Common Sieve Conditions
header
Checks the value of specific email headers.
if header :contains "From" "[email protected]" {
# actions
}
This rule examines the sender address.
address
Checks email addresses found in message headers.
if address :domain :is "From" "example.com" {
# actions
}
This example matches any sender from the example.com domain.
size
Filters messages based on their size.
if size :over 500K {
# actions
}
This condition matches messages larger than 500 KB.
body
Checks the content of the email body.
if body :contains "unsubscribe" {
# actions
}
This rule triggers when the specified text appears within the message content.
Common Sieve Actions
fileinto
Moves a message to a selected mailbox folder.
if header :contains "Subject" "project" {
fileinto "Projects";
}
Messages matching the condition will be moved to the “Projects” folder.
redirect
Automatically forwards a message to another email address.
if address :is "To" "[email protected]" {
redirect "[email protected]";
}
discard
Silently deletes a message without notifying the sender.
if header :contains "Subject" "spam" {
discard;
}
keep
Leaves the message in the Inbox.
if header :contains "Subject" "important" {
keep;
}
reject
Rejects the message and returns an error notification to the sender.
if size :over 10M {
reject "Message too large";
}
Example Sieve Script
The following example demonstrates several common filtering actions:
require ["fileinto", "reject"];
# Move project-related emails
if header :contains "Subject" "project" {
fileinto "Projects";
}
# Forward messages from a specific sender
if address :is "From" "[email protected]" {
redirect "[email protected]";
}
# Reject oversized messages
if size :over 10M {
reject "Message too large";
}
# Remove unwanted messages
if header :contains "Subject" "spam" {
discard;
}
# Keep all remaining messages
keep;
Practical Uses for Sieve Filters
Sieve can be used to automate many common email management tasks, including:
- Sorting invoices into a dedicated folder
- Separating work and personal emails
- Automatically forwarding messages to another address
- Blocking unwanted senders
- Filtering newsletters and mailing lists
- Rejecting oversized attachments
- Organizing support requests into specific mailboxes
Summary
Sieve provides a powerful and flexible way to automate email management directly on the mail server. By combining conditions and actions, you can create advanced filtering rules that keep your mailbox organized and ensure important messages are handled exactly as intended.
Whether you want to sort incoming mail, block unwanted messages, or automate forwarding, Sieve offers a reliable server-side solution that continues to work even when your email client is offline.