Zimbra SkillZ: Using Sieve Filters on Zimbra

Hi Zimbra Customers, Partners & Friends,

Sieve is a powerful scripting language for filtering incoming email messages. This blog is a short how-to for using sieve filters on Zimbra webmail. Zimbra supports incoming email filters set by users, but administrators should set up and install sieve filters.

First, allow adding email headers to help debug your filter scripts:

zmprov mc default zimbraSieveEditHeaderEnabled TRUE

 
Then create a text file with your sieve script. In this example we use /tmp/myfilters:

require ["fileinto", "reject", "tag", "flag", "editheader"];

# add an external domain header to all email not coming from our own domains
if allof(
  not address :domain :is ["from"] ["example.org", "lists.example.org", "otherdomain.nl"]
)
{
  addheader "X-External-Domain" "Warning come from external domain";
}

# restrict anyone that uses example.com to mail to a domain that is not example.nl, 
# but allow mailing to info2@example.com. To notify the sender in case the mail is rejected. 
# Instead of `reject`, you can use `discard`. Discard will not tell the sender the email was 
# not delivered
if allof(
  address :domain :is ["from"] ["example.com"],
  not address :domain :is ["to"] ["example.nl"],
  not address :is ["cc", "to"] ["info2@example.com"]
)
{
  reject "Sorry. Company policy does not allow external mails. Please contact support@org for more information.";
  stop;
}

Here are some more examples:

# filter based on any header containing barry, put it in the barry folder
if anyof (header :contains "from" "barry" )
{
    addheader "X-MyBarry-Header" "mycustomheadervalue";
    fileinto "barry";
    stop;
}

#Filter email based on a subject
if header :contains "Subject" [
  "Logwatch"
  ]
{
    fileinto "Logwatch";
    stop;
}

#Filter based on a custom header, that indicates email was forwarded via a rule in outlook.com
if header :contains "X-MS-Exchange-Inbox-Rules-Loop" [
  "user@hotmail.com"
  ]
{
    fileinto "forwarded-from-outlook";
    stop;
}

# If you do not like sendgrid, you can move it to Junk based on the Return-Path
if header :contains "Return-Path" [
  "sendgrid.net"
  ]
{
    fileinto "Junk";
    stop;
}

#Not doing business in any of these countries, you can use wildcard 
if address :domain :matches ["From"] ["*.za", "*.pe","*.sg","*.id","*.mk","*.cn","*.ua"]
{
  fileinto "Junk";
  stop;
}

Apply the filters to an account like this:

cat /tmp/myfilters |xargs -0 zmprov ma info@example.com zimbraAdminSieveScriptBefore

You can set zimbraAdminSieveScriptBefore per account, COS, domain or server. If you set it on a domain and on an account in that domain, the script on the account is used.

To unset zimbraAdminSieveScriptBefore on an account:

  zmprov ma info@example.com zimbraAdminSieveScriptBefore ""

 
Thanks,
Your Zimbra Team

, , , , ,

2 Responses to Zimbra SkillZ: Using Sieve Filters on Zimbra

  1. Bruce July 14, 2021 at 8:04 PM #

    “…administrators should set up and install sieve filters.”
    Why should we? Wouldn’t this be a duplicative process for each user? What’s the advantage for using Sieve filtering?

    • Barry de Graaff July 26, 2021 at 9:23 AM #

      Sieve filters can be set-up for a group of users in a class of service or for an entire domain. These filters cannot be overridden by the end-user. This can be useful for office automation, example if people use scan-to-mail in the office the email from the scanner can be filed in a separate folder, or you can automate processing based on filters in the email if you use anti-spam/anti-virus appliances. You could also file/filter all social/transactional email in your company to boost productivity. But you can also opt to not use it, it is just a feature.

Leave a Reply