← Back to Research

This document outlines the syntax mapping to translate legacy Sieve (RFC 5228) email filtering rules into AfterSMTP's highly performant, Turing-complete Next-Gen MailScript (Starlark) engine.

MailScript offers complete parity with Sieve while allowing advanced programmatic access to the blockchain ledger and decentralized identity primitives.

1. Core Rule Structure

Legacy Sieve

Sieve rules are evaluated sequentially as a block of if/elsif statements against a hidden state machine.

require ["fileinto", "reject"];

if header :contains "subject" "viagra" {
    discard;
} elsif header :is "from" "boss@company.com" {
    fileinto "Important";
} else {
    keep;
}

Next-Gen MailScript

MailScript is a Starlark environment. All logic must reside within the evaluate() entry point. The engine returns the final action to the AfterSMTP Router.

def evaluate():
    subject = get_header("Subject").lower()
    from_address = get_header("From")

    if "viagra" in subject:
        discard()
    elif from_address == "boss@company.com":
        fileinto("Important")
    else:
        accept()  # equivalent to keep

evaluate()

2. Capability Mapping

Action Legacy Sieve MailScript (Starlark)
Accept/Keep keep; accept()
Discard (Silent) discard; discard()
Reject (Bounce) reject "Spam"; reject("Spam")
Move to Folder fileinto "Junk"; fileinto("Junk")
Forward/Redirect redirect "a@b.com"; redirect("a@b.com")
Out of Office vacation :days 7 "I am out"; vacation(days=7, body="I am out")
Add IMAP Flag setflag "\\Seen"; add_flag("\\Seen")

3. String Matching

Legacy Sieve (Requires extensions)

require ["regex"];
if header :regex "subject" ".*urgent.*" { ... }
if address :domain "from" "example.com" { ... }

Next-Gen MailScript (Native Pythonic matching)

def evaluate():
    # Regex Matching natively via Go re2 bridge
    if regex_match(".*urgent.*", get_header("Subject")):
         add_flag("\\Flagged")

    # Simple Domain checking natively
    domain_str = get_header("From").split("@")[-1]
    if domain_str == "example.com":
         accept()

4. Advanced: Native Blockchain Actions

MailScript introduces routing capabilities impossible in legacy Sieve:

def evaluate():
    # Route securely over QUIC if the ledger identity has an active X25519 key
    recipient_did = get_recipient_did()
    if is_did_active(recipient_did):
         route_quic()
    else:
         route_legacy_smtp()