Utterly failing with Sieve

Your question

I run a mail server for a non-profit and I’m trying to replace the convoluted mess of Postfix, Dovecot and Mailman with Stalwart. As a standard mailserver, its working great but I have three requirements that I can’t seem to make work. I have some experience with Sieve from Dovecot and I’ve been using some examples I found here.

For context, I have several mailing lists that behave like aliases so, for example, mail to [email protected] gets forwarded to the current president’s personal email. Anyone should be able to send messages to those lists. But then I have some others that are, basically, outbound. [email protected] is the big one. For this one I want only the current officers or a “real” eaa27.org account to be able to post. I have the officers in a key-value list. The script I’m using is supposed to add a debug header but I don’t see it in any of my test messages. So my feeling is the script isn’t being called. I am using the condition “rcpt == ‘[email protected]’” in the RCPT TO stage.

list_sender_check

# Reject anyone who's not an officer or not sending from eaa27.org
require ["variables", "envelope", "vnd.stalwart.expressions", "reject"];

eval "add_header('X-script-debug-1', envelope.from)";

if anyof (
  not envelope :domain "from" "eaa27.org",
  not eval "key_exists('officers', to_lowercase(envelope.from))"
) {
  reject "550 5.7.1 Sender not authorized by this mail system.";
}

Next, emulating a Mailman feature, I’d like to add ‘[EAA27]’ to the subject line of any message going out. I’m not sure the best place to implement this one. I tried in the DATA stage with a “true” in the condition but, again, it doesn’t seem to be triggered.

require ["variables", "editheader",  "envelope"];

if header :matches "Subject" "*" {
    set "old_subject" "${1}";
    deleteheader "Subject";
    addheader "Subject" "[EAA Chapter 27] ${old_subject}";
} else {
    addheader "Subject" "[EAA Chapter 27]";
}

The final requirement is to help avoid SPF problems and that is when the President sends a message from his personal email I want the sender to get rewritten as ‘[email protected]’. That’s why I created the key-value list but I’m totally lost as to how to actually implement that.

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

I now have everything needed. Let me check whether script field needs the expression to be true/condition evaluated differently (user mentions using rcpt == '...' directly as the condition, and true for DATA), which may be the actual bug.

Three separate issues, but they likely share one root cause: the script field on each MTA stage is not simply a condition, it’s an expression that must evaluate to the Sieve script’s name (a string), with false meaning “don’t run a script.” If you just put a bare condition like rcpt == '[email protected]' as the value, it won’t ever select your script by name, so it silently never runs.

The correct shape, used consistently across all three stages, is:

{
  "script": {
    "match": [
      {"if": "rcpt == '[email protected]'", "then": "'members_filter'"}
    ],
    "else": "false"
  }
}

This is set on MtaStageRcpt.script for your posting-restriction check (recipient available as rcpt), and your actual logic (checking the officers key-value list, rejecting with reject) lives in a Sieve script named members_filter. See the RCPT stage docs for a full worked example (greylisting), which uses this same match/then/else pattern.

For your three requirements specifically:

  1. Restrict posting to [email protected]: set MtaStageRcpt.script as above, pointing to a script that uses key_exists('officers', envelope.from) (or similar, depending on how you populate your key-value list) to allow/reject. See key-value lookup lists and the key_exists/key_get reference.

  2. Add [EAA27] to the Subject: this belongs in MtaStageData.script (DATA stage, after the full message is available), using editheader’s deleteheader/addheader combo to rewrite the Subject. Example pattern is in the Headers rewriting docs. You’ll need require ["editheader","variables"]; and a header :matches "subject" "*" test to capture and rebuild the value.

  3. Rewrite sender to [email protected]: this is envelope/header rewriting at the MAIL stage. Set MtaStageMail.script (again using the match/then/else expression form) to run a script that checks the sender against your key-value list and does set "envelope.from" "[email protected]"; (plus an addheader/deleteheader on the From: header if you also want the visible header changed). See Address rewriting / Sieve.

Double check each stage’s script expression actually evaluates to a quoted script name string (e.g. 'members_filter'), not a bare boolean — that’s almost certainly why your debug header never showed up.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

I’m using the webui for all this. This is what I’m doing in the RCPT TO section:

Also, for sender replacement I looked at the greylist example, which uses SQL. I haven’t figured out how to do the same thing using key_get. I tried a couple sieve testers but they didn’t understand key_get.

The stage script setting is an expression that returns the name of the script to run, not a condition. With rcpt == '[email protected]' (or true at DATA) as the whole expression, it evaluates to a boolean, which doesn’t name any script, so nothing runs. Put the condition in if and the quoted script name in then. The scripts also need to be marked active, followed by a settings reload (Trusted Interpreter | Stalwart). For the RCPT stage:

{"script":{"match":{"0":{"if":"rcpt == '[email protected]'","then":"'list_sender_check'"}},"else":"false"}}

Even once it runs, the debug header won’t show up: header changes made at RCPT are thrown away, since there’s no message yet, and only envelope changes are kept. Use a temporary reject with the value in the text if you need to see what the script sees there. The logic also needs a fix. anyof (not domain, not officer) rejects unless the sender is both an eaa27.org address and an officer. Also, the envelope domain is whatever the sender claims, so for “a real eaa27.org account” check that the session is authenticated instead:

require ["envelope", "reject", "vnd.stalwart.expressions"];

if not eval "!is_empty(env.authenticated_as) || key_exists('officers', to_lowercase(envelope.from))" {
  reject "550 5.7.1 Sender not authorized to post to this list.";
}

The subject tag belongs at DATA, wired the same way, and recipients lets you limit it to the list:

{"script":{"match":{"0":{"if":"contains(recipients, '[email protected]')","then":"'list_subject_tag'"}},"else":"false"}}

Your tagging script works as it is; I’d only skip messages that already carry the tag, so replies don’t end up with “[EAA Chapter 27] Re: [EAA Chapter 27]”.

For the President, the envelope sender is rewritten with the rewrite expression on the MAIL FROM stage (MtaStageMail | Stalwart). If your key-value list maps the personal address to the role address, it looks like this:

{"rewrite":{"match":{"0":{"if":"key_exists('officers', sender)","then":"key_get('officers', sender)"}},"else":"false"}}

That runs before RCPT, so the list check above will see [email protected] rather than the personal address; make sure the officers check accepts it. It also only fixes SPF. The From header still shows the personal domain, and adding a subject tag breaks that domain’s DKIM signature, so receivers can still fail DMARC if that domain publishes a strict policy. Mailman deals with this by rewriting From to the list address and putting the original sender in Reply-To, which you can do with editheader in the DATA script if it turns out to be a problem.

Thank you. That helped a lot. I had actually caught the anyof vs allof about 10 minutes before I saw your message. Am I correct in thinking that env.authenticated_as will only get a value if someone actually sends via the submission port? I.e. if I send a message from my personal account, which then gets rewritten as ‘[email protected]’ it will still fail the authenticate_as test.

Yes. env.authenticated_as is only filled in after SMTP AUTH, and by default AUTH is only offered on the submission ports over TLS, never on 25. A message you send from your personal provider reaches Stalwart on port 25 unauthenticated, and rewriting the envelope sender doesn’t change that. So for those posts the officers lookup is the only thing that can let them through.

That lookup needs a fix as well, which I missed last time. After the rewrite, envelope.from at RCPT is [email protected], and in the mapping I suggested the role addresses are values, not keys. Adding each role address as a key in officers too (the value doesn’t matter) fixes it. Keep that list all key-value, since a namespace that mixes sets and key-value entries isn’t supported, and reload after editing it.

The envelope sender on port 25 is whatever the sending server claims, so anyone who knows an officer’s personal address can post to members@. If that bothers you, check at DATA instead, at the top of the subject-tag script, where the From header still has the personal address and DMARC has already run:

require ["reject", "extlists", "variables"];
if allof (string :is "${env.authenticated_as}" "",
          not allof (address :all :list "from" "officers",
                     string :is "${env.dmarc.result}" "pass")) {
  reject "550 5.7.1 Sender not authorized to post to this list.";
}

One side effect of the rewrite: it happens before Stalwart’s own SPF check, so those posts are checked against eaa27.org’s SPF from your provider’s servers and fail. With the default relaxed SPF policy on port 25 that only adds a point to the spam score, but it’s a reason not to make SPF strict on that listener.