0

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?

Ralph
  • 1

2 Answers2

1

This works:

require "regex";
if header :regex "to" "@[^(example.com)]+" { keep; } else { discard; }
Tom
  • 11
  • 1
  • 1
    please provide more support for the answer. it may answer the question, but without explanation it will not work – djdomi Dec 31 '21 at 21:28
0

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

Eric
  • 101
  • 2