Moving from an ancient mailserver setup I just set up Stalwart 1.0 on a new machine, and started working on moving over most of the old setup.
Part of the old setup is a combination of multiple milters, some of which have become obsolete (smf-spf e.g.) due to built-in features.
Of the ones remaining, one does extensive rewriting of recipient addresses (GitHub - GarryG/FlexiMilter: A flexible, DB-backended milter to allow for complex mail handling · GitHub, though with several yet unpublished changes to work with Stalwart), and two (one historical, one new) to archive incoming mails. Typical for mail archiving, I wouldn’t want - say - two dozens of different recipients in the archived mails, if all are redirected to the same local mailbox. Therefore, it’s rather important that I can control the sequence in which the mails are run through the milters, with the rewriting milter early on in the processing.
While I was able to put them all into Stalwart, I seem to be missing any ordering functionality. In the list shown in the GUI, they are just ordered by their IP addresses (or rather, the port numbers it seems).
Did I miss something as far as the sequence is concerned? Is there some workaround I can make to ensure my rewrite happens first (e.g., give it a lower port number?)
Thanks!
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.
Stalwart has no ordering field for milters. According to the Milter filters docs, when several milters are defined they are consulted in the order in which they were created.
The GUI list order (it looks sorted by port or address to you) is only a display order. It doesn’t set the execution order, so changing the port number won’t help. I haven’t confirmed whether object IDs are the actual sort key, so I’d treat creation order as the rule.
To get your rewriting milter to run first, delete the other milters and recreate them after it. The order would then be:
Rewrite milter
Historical archive milter
New archive milter
The rewrite milter has to see the recipients before the archivers do. Make sure it acts at the data stage, because the docs say changes to the message are applied only there. The default stages value is data only. Envelope changes from earlier stages are not applied.
Also, with several milters, only the last body replacement is kept. That matters if one of your archivers rewrites the body.
This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.
Interestingly, the one I created first is the last on the list in the GUI, haven’t confirmed whether the sequence of creation actually works … anyway, while this info makes the problem deterministic, you can’t tell from the web interface, and adding some kind of sorting mechanism (either with a priority value per milter, or just moving them around in the GUI) would be a helpful feature…
Changing the sequence won’t give you the archive you’re after, sadly. Every DATA-stage milter gets the same envelope and message: Stalwart collects the changes from all of them and applies them once the last one has finished. Your archive milters would see all two dozen original recipients even with FlexiMilter running first.
What does work is rewriting before the DATA stage, with the rewrite expression on the RCPT stage (RCPT TO stage | Stalwart). It runs as each recipient is accepted, so the milters at DATA get the rewritten address, and recipients that end up at the same address collapse into one. It can read from a database through a store lookup, so your FlexiMilter tables may be usable as they are. Something along these lines, with the namespace, query and placeholder adjusted to your database:
A result without an @ leaves the recipient unchanged, so addresses with no row pass through. This only maps one address to one address. If FlexiMilter also adds recipients, that part can’t move to the RCPT stage, and the archive step would have to be called from FlexiMilter itself.
On the sequence itself, creation order is what the server uses, and the web interface lists milters newest first, so the one at the bottom of your list runs first. Deleting and recreating them is the only way to change it. A priority field, or having later milters see earlier changes, would be a good topic for Feature Ideas: Feature Ideas - Stalwart Support