snippets

How do I add a header that only SpamAssassin or rspamd can see?

action_insert_header_now and the other action_*_header_now functions patch ./INPUTMSG immediately, so a later spam_assassin_check or rspamd_check in the same filter run sees the change. Since MIMEDefang 3.8 they accept an optional last inputmsg_only argument: when it is true only ./INPUTMSG is changed, and the message delivered by the MTA is left untouched. This is handy to pass the scanners information that recipients should not see, such as the result of an earlier check.

sub filter_end {
    my ($entity) = @_;

    # Seen by SpamAssassin, not added to the delivered message;
    # pass undef to use the default position
    action_insert_header_now('X-Relay-Country', $country, undef, 1);

    # Hide a forged X-Spam-Status from the scanner only
    action_delete_header_now('X-Spam-Status', undef, 1);

    my ($hits, $req, $names, $report) = spam_assassin_check();
    # ...
}

Write a SpamAssassin rule that matches the header (for example header RELAY_IN_XX X-Relay-Country =~ /^XX$/) to give it a score.