Entourage: Spam filter

I got really fed up with spam that sneaks by keyword filters by using subject lines like “A+D;U)L`T+ V_I &D#E 0)S”. So I wrote this filter that takes the ratio of letters to non-letters and zaps the email if it exceeds a threshold. The space character doesn’t count in the calculation. I arbitrarily chose 10%, but this can be adjusted based upon the types of email one receives. I might adjust it myself if I find it too strong or too weak. I’m using Entourage, but it can easily be modified for other mail programs.

Caveat: This code permanently deletes, so if you don’t want that, remove the second delete.


on run
        tell application "Microsoft Entourage"
                
                -- delete messages that have <threshold> percentage of characters
                -- outside the range of A..Z and a..z (and space) in the subject line.
                -- This is to zap emails that substitute special character for letters, to get
                -- past spam filters that drive off key words.  Threshold can be
                -- adjusted as you see fit.  I arbitrarily chose .10.  Also, the characters
                -- included or excluded in either category can be easily changed.
                -- NOTE:  This permanently deletes, so if you don't want that, just
                -- remove the 2nd delete below.
                
                set CurrMessages to the current messages
                set alphaChar to 0 as integer -- number of Alpha characters and Space
                set otherChar to 0 as integer -- number of non Alpha characters
                set otherRatio to 0.0 as real -- otherChar / alphaChar
                set thresh to 0.10 as real -- adjust this to whatever works
                
                repeat with theMsg in the CurrMessages
                        set subj to the subject of theMsg
                        
                        repeat with thisCharacter in the characters of subj
                                set thisCharacter to thisCharacter as text
                                if (thisCharacter ≥ "A" and thisCharacter ≤ "Z") or (thisCharacter ≥ "a" and thisCharacter ≤ "z") or (thisCharacter = " ") then
                                        set alphaChar to alphaChar + 1
                                else
                                        set otherChar to otherChar + 1
                                end if
                        end repeat
                        
                        set otherRatio to otherChar / alphaChar
                end repeat
                
                if otherRatio > thresh then
                        delete theMsg -- put in Deleted Folder
                        delete theMsg -- delete permanently
                end if
        end tell
end run