Apple Mail - Server Junk Mailbox

Summary: Automatically applies SpamSieve to unread messages in server Junk mailboxes.
Requires: SpamSieve, Apple Mail
Install Location: ~/Library/Application Scripts/ or ~/Library/Scripts/Applications/Mail/
Last Modified: 2015-04-09


The junk/spam filter on your mail server can cause problems. It is more likely than SpamSieve to mistakenly classify your good messages as spam, yet it rarely catches spam messages that SpamSieve would have missed. Additionally, most people don’t want to deal with two separate spam mailboxes, one for the server junk filter and one for SpamSieve. It’s often best simply to turn off the server junk filter, however some mail servers such as iCloud do not allow this.

This script solves two problems:

  1. It saves you from mistakes that the server junk filter made by moving any messages that it thinks are good back to the inbox.
  2. It consolidates the spam messages from all the accounts into a single spam mailbox (the same one where SpamSieve is already putting your spam).

To install:

  1. Download the script using one of the links below. On OS X 10.8 Mountain Lion or later, the script must be saved in the folder /Users/<username>/Library/Application Scripts/ This is because Mail is sandboxed and only has access to run scripts in that folder. To access the Library folder, click on the Go menu in the Finder while holding down the Option key.
  2. Open the script in AppleScript Editor and enter your account and junk mailbox names in accountAndServerJunkMailboxNames(). The account name comes from the Description field in the Accounts tab of Mail’s preferences. For example, if you have one account called Account 1 and want to process its Junk mailbox, you would use:

    on accountAndServerJunkMailboxNames()
        return {{"Account 1", {"Junk"}}}
    end accountAndServerJunkMailboxNames

    You can also get the AppleScript names using the Apple Mail - List Mailboxes script.

    If you want to process the Junk mailbox of the Personal account and the Junk and Bulk mailboxes of the Work account you would use:

    on accountAndServerJunkMailboxNames()
        return {{"Personal", {"Junk"}}, {"Work", {"Junk", "Bulk"}}}
    end accountAndServerJunkMailboxNames
  3. By default, SpamSieve consolidates the spam in a mailbox called Spam under On My Mac. This configuration is specified using:

    on mainSpamMailboxInfo()
        return {"", "Spam"}
    end mainSpamMailboxInfo

    You can also specify a particular mailbox in one of your accounts. For example, if you had created a SpamSieveSpam mailbox in the Personal account you could specify that using:

    on mainSpamMailboxInfo()
        return {"Personal", "SpamSieveSpam"}
    end mainSpamMailboxInfo
  4. Go to Mail’s Preferences window and create a new rule at the top of the list (above the SpamSieve rule) called Server Junk Mailbox SpamSieve.
  5. The rule conditions should say Every Message.
  6. The rule actions should say Run AppleScript […]apple-mail-server-junk-mailbox.scpt. First, choose Run AppleScript from the pop-up menu; then select the apple-mail-server-junk-mailbox.scpt file (using either the pop-up menu or the Choose… button).

Now, whenever you receive a new message in the inbox, SpamSieve will look in all the server junk mailboxes that you specified and check the unread messages. Any that are spam will be moved to the Spam mailbox. Any that are good will be moved to the inbox.

Normally, the script will run when Mail receives a new message in the inbox and applies the “Server Junk Mailbox SpamSieve” rule. You can also set up the script as a standalone application. This has less overhead than running the script from within Mail, and it allows the script to run on a more predictable schedule. To do this, use Script Editor to save the script as an application (with the Stay Open option checked) and then launch the application. You can set in the “idle” handler how often it should run.

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


property pMarkSpamMessagesRead : false
property pColorSpamMessages : true
property pFlagSpamMessages : false
property pMoveBlueMessagesToTrash : false
property pMoveGrayMessagesToTrash : false
property pMovePurpleMessagesToTrash : false
property pMoveRedMessagesToTrash : false
property pMoveOrangeMessagesToTrash : false
property pMoveYellowMessagesToTrash : false

on accountAndServerJunkMailboxNames()
return {{"Account 1", {"Junk"}}}
end accountAndServerJunkMailboxNames

on mainSpamMailboxInfo()
return {"", "Spam"}
end mainSpamMailboxInfo

-- Do not modify below this line.

on run
-- This is executed when you run the script directly.
my filterServerJunkMailboxes()
end run

on idle
-- This is executed periodically when the script is run as a stay-open application.
my filterServerJunkMailboxes()
return 60 * 5 -- Run again in 5 minutes.
end idle

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

on filterServerJunkMailboxes()
tell application "Mail"
repeat with _pair in my accountAndServerJunkMailboxNames()
set {_accountName, _mailboxNames} to _pair
set _account to account _accountName
repeat with _mailboxName in _mailboxNames
my filterAccountMailboxNamed(_account, _mailboxName)
end repeat
end repeat
end tell
end filterServerJunkMailboxes

on filterAccountMailboxNamed(_account, _mailboxName)
tell application "Mail"
set _mailbox to _account's mailbox _mailboxName
set _spamMailbox to my mainSpamMailbox()
set _inbox to my inboxFromAccount(_account)
set _messages to messages of _mailbox whose read status is false and deleted status is false
repeat with _message in _messages
if not my processMessageIfSpam(_message, _spamMailbox) then
set _message's mailbox to _inbox
end if
end repeat
end tell
end filterAccountMailboxNamed

on processMessageIfSpam(_message, _spamMailbox)
tell application "Mail"
set _source to _message's source
end tell
tell application "SpamSieve"
set _score to score message _source
end tell
tell application "Mail"
set _isSpam to _score ≥ 50
set _message's junk mail status to _isSpam
if _isSpam and pMarkSpamMessagesRead then
set _message's read status to true
end if
set _moveToTrash to my colorMessageAndDecideIfShouldMoveToTrash(_message, _score)
if _moveToTrash then
delete _message
else if _isSpam then
set _message's mailbox to _spamMailbox
end if
return _isSpam
end tell
end processMessageIfSpam

on colorMessageAndDecideIfShouldMoveToTrash(_message, _score)
tell application "Mail"
set _table to
blue, pMoveBlueMessagesToTrash, 6}, ¬
gray, pMoveGrayMessagesToTrash, 5}, ¬
purple, pMovePurpleMessagesToTrash, 4}, ¬
red, pMoveRedMessagesToTrash, 3}, ¬
orange, pMoveOrangeMessagesToTrash, 2}, ¬
yellow, pMoveYellowMessagesToTrash, 1}, ¬
none, false, -1}}
-- Flag colors chosen so that messages sort by spamminess: gray, purple, blue, green, yellow, orange, none
repeat with _row in _table
set {_threshold, _color, _moveToTrash, _flagColor} to _row
if _score_threshold then
if pColorSpamMessages then
set _message's background color to _color
end if
if pFlagSpamMessages then
set _message's flag index to _flagColor
end if
return _moveToTrash
end if
end repeat
end tell
end colorMessageAndDecideIfShouldMoveToTrash

on mainSpamMailbox()
set {_accountName, _mailboxName} to my mainSpamMailboxInfo()
tell application "Mail"
if _accountName is "" then
return mailbox _mailboxName
return mailbox _mailboxName of account _accountName
end if
end tell
end mainSpamMailbox

on inboxFromAccount(_account)
tell application "Mail"
set _names to {"INBOX", "Inbox", "innboks", "Posteingang", "Boite de reception"}
repeat with _name in _names
set _mailbox to mailbox _name of _account
return _mailbox
end try
end repeat
return inbox
end tell
end inboxFromAccount