A role to send only "local" mail

Your question

Is it possible to set a group so a mail user can only send mails to local receipients?
For example: [email protected] could only send mails to {username}@mydomain.com but not to {username}@{anydomain}.com.

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

mail.md is the MAIL FROM stage doc - relevant for submission-side filtering. Let me check it, and check sieve at RCPT stage for rejecting based on domain, plus roles/permissions docs.

You can enforce this with the allowRelaying expression on the MtaStageRcpt object (RCPT TO stage), since it is evaluated per-recipient and has access to both authenticated_as and rcpt_domain. It isn’t a “role” or group permission as such, but an expression lets you block a specific user (or list of users) from relaying to non-local domains while leaving everyone else unaffected:

{
  "allowRelaying": {
    "match": {
      "0": {
        "if": "authenticated_as == '[email protected]' && !is_local_domain(rcpt_domain)",
        "then": "false"
      }
    },
    "else": "!is_empty(authenticated_as)"
  }
}

This rejects relaying to any domain other than one registered as local (is_local_domain) when the authenticated sender is [email protected], but keeps the default “allow when authenticated” policy for everyone else.

To cover several accounts instead of one, you can store the restricted usernames in an in-memory store (e.g. a list named local-only) and test membership with key_exists('local-only', authenticated_as) instead of a literal equality check.

Reference: RCPT stage / Relay and MtaStageRcpt.

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

Is it possible to use "In-Memory Keys"?

Yes, that works. Create a MemoryLookupKey for each account you want to restrict, with namespace local-only and the account name as the key, then add a system Sieve script and set it as the RCPT stage script (RCPT stage | Stalwart):

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

if eval "!is_empty(env.authenticated_as) && key_exists('local-only', env.authenticated_as) && !is_local_domain(email_part(envelope.to, 'domain'))" {
    reject "550 5.7.1 You are not allowed to send to external recipients.";
}

The script can’t see group membership, so the keys have to be account names rather than a group. Keep the code at the start of the reject text; without it the client gets a generic 503.