Apple Mail - Block Sender

Summary: Adds rules to SpamSieve’s blocklist for the selected message’s sender.
Requires: SpamSieve, Apple Mail
Install Location: ~/Library/Scripts/Applications/Mail
Last Modified: 2019-10-02


This script is not intended for most customers because the “Train as Spam” command already blocks senders.

Normally, when you train a message as spam, SpamSieve will create blocklist rules for the sender name and address and also train SpamSieve’s Bayesian classifier. This script is for just creating the blocklist rules, when SpamSieve has already correctly classified the message as spam (so it’s not necessary to use the “Train as Spam” command), but you still want to make sure SpamSieve knows that the sender is very spammy. This ensures that future messages from that sender will go directly to the trash without excessively training the Bayesian classifier and filling up the corpus.

Installation Instructions · Download in Compiled Format · Download in Text Format


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

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

on processMessage(_message)
tell application "Mail"
set _address to extract address from _message's sender
set _name to extract name from _message's sender
end tell
tell application "SpamSieve"
tell blocklist
-- SpamSieve automatically prevents duplicate rules
make rule with properties {text to match:_address, match field:from field, match style:exact style}
make rule with properties {text to match:_name, match field:from name field, match style:exact style}
end tell
end tell
end processMessage

on logToConsole(_message)
set _logMessage to "SpamSieve [Apple Mail Block Sender] " & _message
do shell script "/usr/bin/logger -s " & _logMessage's quoted form
end logToConsole