Score:0

Sieve: Filter Rules

in flag

I want to implement a rule as follows:

  • If "To" comprises only addresses having "*@example.com" -> discard
  • Otherwise -> keep

In other words:

  • If "To" comprises at least one address different to "*@example.com" -> keep
  • Otherwise -> discard

The problem with the statement if not header :contains "To" "@example.com" { keep; } else { discard;} is that a mail is not kept if there is a "*@example.com" address among other non-"*@example.com" addresses.

What I would need is an option to negate the search pattern rather than the complete statement, e.g.: if header :contains "To" NOT "@example.com" { keep; } else { discard;}

Any ideas how to solve this?

Score:1
ye flag
Tom

This works:

require "regex";
if header :regex "to" "@[^(example.com)]+" { keep; } else { discard; }
djdomi avatar
za flag
please provide more support for the answer. it may answer the question, but without explanation it will not work
Score:0
de flag

There is a :count match type (like :is and :contains, etc.) in the "relational" extension you can use with the :comparator comparator to do this, and in fact your use-case is the standard example for it!

If you're using Dovecot Pigeonhole (used by many package solutions and providers like RoundCube, FastMail, and others), the "relational" extension comes with it, you just have to require it (and its comparator; see below) at the top of your sieve file with your others. The sieve filter should look the same no matter what implementation you're using, as long as the extensions are supported.

IANA maintains the master list of official extensions. If you use Dovecot Pigeonhole Sieve, scroll down to "Implementation Status" on its Github to see which ones are supported there (all are, most completely). This means you don't have to search to find the "relational" extension.

Note that while :comparator is supported by the base install (of Dovecot's implementation), it doesn't include numerical comparison by default and the collation itself is an extension that must be required specificially.

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

# ...

# reject if the number of recipient addresses is greater than 1
if allof(
  address :domain ["to","cc"] "example.com",
  address :count "gt" :comparator "i;ascii-numeric" ["to","cc"] "1"){
    # if you really want it destroyed
    # discard;
    fileinto :create "Trash"; stop;
  }

I did some tests and the filter will combine the counts of TO & CC (but not BCC) for the comparison, so it applies to the total number of (visible) recipient addresses.

RFC 5228: Sieve
RFC 5231: Relational Extension

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.