Sieve Email Filtering: MIME Part Tests, Iteration, Extraction, Replacement, and Enclosure
Extracted elements (26)
After 'enclose' is executed, subsequent actions and tests operate on the newly created wrapper message rather than the original. Therefore, any 'replace' or similar actions should be executed before 'enclose' to affect the original message content.
When a 'foreverypart' loop replaces a multipart MIME part via 'replace', structural changes take effect immediately: the currently executing iterator does not descend into the now-removed children, and subsequent iterators use the new structure. This avoids undefined behavior from iterating over deleted parts.
The ':anychild' tagged argument may only appear when ':mime' is also present; its presence without ':mime' is an error. Without ':mime' and ':anychild', the extended 'header', 'address', and 'exists' tests behave identically to their RFC 5228 definitions.
If ':mime' is specified in 'replace' along with either ':subject' or ':from', those parameters MUST be ignored. This SHOULD be flagged as a compilation error.
Implementations SHOULD support desegmentation, decoding, and charset translation of parameter values encoded per RFC 2231 as part of the ':mime' header test. Implementations MAY also decode RFC 2047-encoded words in parameter values.
Implementations supporting the 'editheader' extension MUST ensure that Original-Subject or Original-From headers added by 'replace' cannot be modified or removed via 'editheader'.
Servers MUST support transcoding of any textual body part into UTF-8 for use with 'extracttext', including decoding transfer encodings and transcoding from the indicated character set. Unrecognized or invalid encoding results in an empty string.
Sieve implementations MAY limit the number of nested 'foreverypart' loops, but MUST support at least one nested loop inside another loop.
Using 'extracttext' outside a 'foreverypart' loop sets the variable to empty string and SHOULD be flagged as a compilation error.
When 'replace' is used without ':mime', the replacement is treated as a text/plain part in UTF-8. When ':mime' is specified, the replacement string is a full MIME entity including MIME headers and content.
When 'replace' uses ':from', implementations SHOULD check syntax and generate an error for syntactically invalid values. Implementations MUST preserve the previous From header as Original-From if the From header is changed.
When replacing the entire message with a ':subject' parameter, implementations MUST convert non-ASCII characters to RFC 2047-encoded words, MUST preserve the previous Subject as Original-Subject, and MUST preserve all other header fields except those relating to the replaced MIME structure.
MIME header options for the ':mime' tagged argument include ':type' (MIME type or Content-Disposition value), ':subtype' (MIME subtype), ':contenttype' (combined type/subtype), and ':param' (named MIME parameters). These are used with the extended 'header' test.
The 'address' test is extended with ':mime' and ':anychild' tagged arguments, causing it to parse MIME header lines as address headers. Without ':mime'/':anychild', it operates only on top-level header fields as in RFC 5228.
The 'break' command terminates the closest enclosing 'foreverypart' loop, or if a ':name' parameter is given, terminates the nearest enclosing loop with that matching name. It is an error to use a named break with no enclosing loop of that name.
The 'enclose' action wraps the entire message as a message/rfc822 attachment inside a new multipart/mixed message with a text/plain preamble. The ':subject' argument sets the new subject; ':headers' copies specified headers from the original message.
The 'exists' test is extended with ':mime' and ':anychild' tagged arguments to check for the existence of MIME headers in MIME parts. Semantics of ':anychild' interaction with 'foreverypart' mirrors the extended 'header' test.
The 'extracttext' action stores the transcoded text content of the current MIME body part into a Sieve variable. Optional ':first N' limits extraction to the first N characters. It must be used within a 'foreverypart' loop; outside a loop, it sets the variable to empty string.
The 'foreverypart' action is a loop iterator that walks through every MIME part of a message depth-first, including nested parts, applying a command block to each. It can be named with ':name' and terminated early using the 'break' command.
The 'header' test is extended with ':mime' and ':anychild' tagged arguments. ':mime' enables parsing of MIME header lines; ':anychild' (only valid with ':mime') causes the test to examine all nested MIME body parts rather than just the current part.
The 'replace' action replaces a MIME part with supplied text. When inside 'foreverypart', it replaces the current MIME part; when outside, it replaces the entire message. Optional ':subject' and ':from' parameters apply only when replacing the entire message.
Five new Sieve extension capability strings are registered with IANA: 'foreverypart' (loop iteration), 'mime' (MIME-aware tests), 'replace' (MIME part replacement), 'enclose' (message enclosure), and 'extracttext' (text extraction into variables).
The Original-Subject and Original-From header fields are registered in the IANA Permanent Message Header Fields registry, created when 'replace' preserves prior Subject or From headers during whole-message replacement.
The 'enclose' action creates an entirely new message, so site policies applicable to message submission should be enforced. Using 'replace' or 'enclose' may interfere with digital signatures (DKIM, S/MIME, PGP, OpenPGP) that cover the message content.
The system MUST be sized and restricted so that even malicious use of MIME part matching does not deny service to other users. It is not possible to examine the MIME structure of decrypted content in a multipart/encrypted MIME part.
When 'enclose' is used on a message containing a multipart/signed MIME part, the implementation MUST copy the original message octet-for-octet to maintain the validity of the digital signature.