Filter before apply rule

Greetings. I am using version. 3.1.1 on a Mac with system 15.4.1 and Mail Version 16.0. The mail filtering is working fine, but I would like to know if it is possible to filter spam with SpamSieve before applying my own rule which forwards the mail to a second account.

I don’t want the spam mail to reach that second account but right now, Mail sends the mail before SpamSieve filters it.

Any idea?

Regards.

I recommend updating to SpamSieve 3.1.2. There is no way (with macOS 14 and later) to apply SpamSieve before Mail rules. Some possible workarounds:

  • You could modify the rule to use an AppleScript that asks SpamSieve whether the message is spam and only forwards it if it’s not.

  • You could trigger the forwarding using some other means, after the messages have been filtered.

Thanks for the reply, Michael. My knowledge of Mac internals is not good enough to try those workarounds, but I will keep looking for a solution.

Kind regards.

PS: I have followed your recommendations and have already upgraded.

You could save this script in the folder ~/Library/Application Scripts/com.apple.mail and then change your rule’s action to Run AppleScript and select this script, and then it will forward only the good messages. You will need to specify the destination address towards the bottom of the script.

on run
    tell application "Mail"
        set _messages to the selection as list
        my processMessages(_messages)
    end tell
end run

using terms from application "Mail"
    on perform mail action with messages _messages
        -- This is executed when Mail runs the rule.
        my processMessages(_messages)
    end perform mail action with messages
end using terms from

on processMessages(_messages)
    repeat with _message in _messages
        try
            my processMessage(_message)
        on error _errorMessage
            my logToSpamSieve("Error processing message: " & _errorMessage)
        end try
    end repeat
end processMessages

on processMessage(_message)
    tell application "Mail"
        set _source to source of _message
    end tell
    tell application "SpamSieve"
        set _origin to {source identifier:"com.c-command.spamsieve.apple-mail.forward-good-messages"}
        set _score to score message _source origin _origin with suppressing notification without auto training
    end tell
    if _score ≥ 50 then
        return -- Stop processing
    end if
    my processGoodMessage(_message)
end processMessage

on processGoodMessage(_message)
    tell application "Mail"
        -- Seems to be macOS bug where it opens the window, anyway
        set _newMessage to forward _message without opening window
        tell _newMessage
            make new to recipient with properties {address:"insert your destination address here"}
        end tell
        send _newMessage
    end tell
end processGoodMessage

on logToSpamSieve(_string)
    try
        tell application "SpamSieve"
            add log entry source "com.c-command.spamsieve.apple-mail.forward-good-messages" message _string
        end tell
    end try
end logToSpamSieve
1 Like

Thank you very very much @Michael_Tsai. I’ll try it as soon as I can.

Kind regards.