Is there any way to process emails in Junk mailbox?

I have hundreds of emails in my Junk folder. Today I had 660. From what I have read, this is done at Apple’s server. Almost none of them have the color coding so I think Spam Sieve doesn’t see them at all because they don’t go to the inbox. The problem: I have a flood of emails to my Junk folder. I still have to look through them because there is an occasional good email in there.

This is actually why I got Spam Sieve. So is there any way to sort through the Junk email box? Because if not, the app isn’t actually doing anything for me. :frowning: Only like three of the 660 had color coding.

The main functionality is to get spam out of your inbox, but you can also use it to rescue good messages that a server filter moved to Junk.

Okay, so just to be clear, Spam Sieve can take messages that are in the inbox and put them in the trash if I label them as junk. Future messages from e.g. Offshore Casino! will go directly to the trash and I never have to look at them again.

If the messages are already in the junk mailbox (because of Apple’s server filter), Spam Sieve can mark them as not junk.

What Spam Sieve can’t do is look through the messages that are already in my junk mailbox and move them to trash. Correct? So if I have 660 messages in my junk mailbox, I have to review them all myself. There isn’t any override or way to reprocess those messages. Offshore Casino! gets my eyeballs every time.

Yes, and move them back to the inbox.

The rescue setup will reprocess those messages and assign them colors. The default behavior is to leave all the spams in the Junk mailbox, but there are also settings you can customize in the script to move certain colors to Trash instead. For example, if you wanted the most spammy blue messages to be moved from Junk to Trash you could edit the line in the script from:

property pMoveBlueMessagesToTrash : false

to:

property pMoveBlueMessagesToTrash : true

Or, you could even set all the colors to move to Trash.

1 Like

That sounds promising. How do I get to the rescue setup? How do I change the script?

I am not seeing this in any of the menus of SpamSieve or in SpamSieve help.

You can download the script here.

Michael,
I so much appreciate this conversation because I have a similar request and I couldn’t figure out how to achieve it. I downloaded the script and I want to provision the property(s) such that every message found in a junk folder is moved back to the inbox, so that the spam sieve rule can run on all messages that might have been previously read from a junk folder. My spam sieve folder is named spamsieve so as not to collide with a junk folder. My apple script skills are weak, and I tried to read the logic to determine which property’s need to be changed from true to false or vice versa. Can you, without much effort, let me know the property values to achieve what I want ?
Thanks - Jonathan Liss

Micheal,
If there is no combination of property’s to achieve what I want, I suspect I will need to clone the entire script and modify the filterMailbox section to always move messages from a junk folder to the inbox.
While I am not fluent in apple script, I probably can figure out what to do.
I think, frankly, this is the best solution, because it is the most generic, as it will undo any server’s spam filter actions. It will move the messages, no matter the state, back into the inbox, then run the original spam sieve algorithms.
Jonathan Liss

on filterMailbox(_mailbox)

tell application “Mail”

my debugLog(my makeLogMessage(“Start checking mailbox”, _mailbox, “”))

set _total to count of messages of _mailbox

my debugLog(my makeLogMessage(“Total messages in mailbox”, _mailbox, _total))

my debugLog(my makeLogMessage(“Getting unprocessed messages in mailbox”, _mailbox, “”))

with timeout of 3 * 60 seconds

set _messages to messages of _mailbox whose read status is false and deleted status is false and background color is none

end timeout

my debugLog(my makeLogMessage(“Messages to process in mailbox”, _mailbox, count of _messages))

repeat with _message in _messages

if not my processMessageIfSpam(_message) then

if pMarkGoodMessagesRead then set _message’s read status to true

my debugLog("Moving message to inbox: " & _message’s subject)

move _message to inbox

end if

end repeat

my debugLog(my makeLogMessage(“Finished checking mailbox”, _mailbox, “”))

end tell

end filterMailbox

Michael,
All completed - I modified your script and of course renamed it. Just two lines were modified and it now moves all mail found in the junk folders back to inbox. I will use as an Apple app for a while and once I’m convinced it works, I will add as a rule above the spam sieve rule.

This is just what I was trying to do for weeks. I do not know apple script but using your script as a base enabled me to do what I wanted. So thank you so much for providing that apple script. I tried short cuts to no avail, but this solution is so much better.
Jonathan Liss

This is what the old Apple Mail - Server Junk Mailbox script is designed to do. However, my recommendation is to have SpamSieve use the Junk mailbox, too, and then you would be able to use the Apple Mail - Rescue Good Messages script unmodified.

OK, I modified the spam sieve rule to point to the All Junk mailbox, but the issue as to why I moved away from it, to a standalone spam box is that there is no all junk mailbox on iPhone. I will then try your script unmodified.

There is no All Junk on iOS, but All Junk is just a built-in smart folder for showing the contents of the Junk mailboxes, which do exist on iOS. You just need to make sure that each account’s Mailbox Behaviors is configured to use a server mailbox.

Okay, we are definitely onto something here. I ran the script with all the colors set to true (I will paste in the script I ran below). Out of 1062 messages in Junk, there were 325 left. A bunch moved to Trash, though they were still marked unread. There were still some emails marked by SpamSieve in the Junk mailbox.

A bunch also moved to my inbox, because either they weren’t spam (I needed to unsubscribe from a bunch of things) or they weren’t identified as such by SpamSieve (I identified a bunch). I did the above for a few days.

I ran the script again today, and out of 455 emails in the Junk folder, 265 were moved to trash. However, of the 190 left in Junk, at least half of them were marked with color bars by SpamSieve and so should have been moved to trash. Running the script again did not move any more messages.

So a few questions:

(1) What should I change in the script to mark the messages moved to Trash as read? Right now it is kind of useful to see which ones are unread, as those are probably the ones moved by the script. But once everything is working, I will want to have those marked as read.

(2) What is up with the emails marked by SpamSieve with color bars, in the Junk folder, that don’t move? I can’t figure out anything different about them. They are all gray and blue, but most junk messages are. The dates are from March 30 to April 10. All of the untagged junk is since then.

(3) I have both marked and unmarked junk from the same email: infona@namr.motorolasolutions.com (22 messages). The unmarked junk is later, so I am not sure why that is happening. I also have a million (161 messages) from info.nnn@nnnnn.savethekoala.com. The "n"s are numbers, so these aren’t identical, but it seems like anything from savethekoala.com should be marked.

(4) Similar to (3): I have 17 messages that have the name listed as “Sex Secrets”, some from motorola and some from koala. Marked and unmarked. Shouldn’t they all be marked by now? Is there a black list? I am confident I will never need an email from Sex Secrets.

Here is the script. All I did was change all the colors to true:

– Apple Mail - Rescue Good Messages

Apple Mail - Rescue Good Messages - SpamSieve AppleScripts

– Summary: Automatically filter messages caught by server junk filters, moving the good messages to the inbox.

– Requires: SpamSieve, Apple Mail

– Install Location: ~/Library/Application Scripts/com.apple.mail

– Last Modified: 2022-12-29

property pMarkSpamMessagesRead : false

property pMarkGoodMessagesRead : false

property pChangeJunkStatus : true

property pColorSpamMessages : true

property pFlagSpamMessages : false

property pUnflagSpamMessages : false

property pMoveBlueMessagesToTrash : true

property pMoveGrayMessagesToTrash : true

property pMovePurpleMessagesToTrash : true

property pMoveRedMessagesToTrash : true

property pMoveOrangeMessagesToTrash : true

property pMoveYellowMessagesToTrash : true

property pEnableDebugLogging : false

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()

if application “Mail” is not running then return

try

tell application “Mail” to get version

on error _error number _errorNumber

if _errorNumber is -1743 then – errAEEventNotPermitted

set _alertMessage to "You can give “Apple Mail - Rescue Good Messages” access to control Mail and SpamSieve from System Preferences > Security & Privacy > Privacy > Automation. For more information, please see:

SpamSieve Manual: Security & Privacy Access"

display alert _error message _alertMessage

end if

end try

tell application “Mail”

try

set _mailboxes to mailboxes of junk mailbox

repeat with _mailbox in _mailboxes

try

my filterMailbox(_mailbox)

on error _error number _errorNumber

my logToConsole(“Error " & _errorNumber & " filtering mailbox “” & _mailbox’s name & “” of account “” & _mailbox’s account’s name & "”: " & _error)

end try

end repeat

on error _error

my logToConsole("Error filtering junk mailboxes: " & _error)

end try

end tell

end filterServerJunkMailboxes

on filterMailbox(_mailbox)

tell application “Mail”

my debugLog(my makeLogMessage(“Start checking mailbox”, _mailbox, “”))

set _total to count of messages of _mailbox

my debugLog(my makeLogMessage(“Total messages in mailbox”, _mailbox, _total))

my debugLog(my makeLogMessage(“Getting unprocessed messages in mailbox”, _mailbox, “”))

with timeout of 3 * 60 seconds

set _messages to messages of _mailbox whose read status is false and deleted status is false and background color is none

end timeout

my debugLog(my makeLogMessage(“Messages to process in mailbox”, _mailbox, count of _messages))

repeat with _message in _messages

if not my processMessageIfSpam(_message) then

if pMarkGoodMessagesRead then set _message’s read status to true

my debugLog("Moving message to inbox: " & _message’s subject)

move _message to inbox

end if

end repeat

my debugLog(my makeLogMessage(“Finished checking mailbox”, _mailbox, “”))

end tell

end filterMailbox

on processMessageIfSpam(_message)

set _source to my sourceFromMessage(_message)

tell application “SpamSieve”

set _score to score message _source without auto training

end tell

tell application “Mail”

my debugLog("Spam score of message is " & _score & ": " & _message’s subject)

set _isSpam to _score ≥ 50

if pChangeJunkStatus then

set _message’s junk mail status to _isSpam

end if

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

my debugLog("Leaving spam message in Junk: " & _message’s subject)

end if

return _isSpam

end tell

end processMessageIfSpam

on colorMessageAndDecideIfShouldMoveToTrash(_message, _score)

tell application “Mail”

set _table to

{99, blue, pMoveBlueMessagesToTrash, 6}, ¬

{95, gray, pMoveGrayMessagesToTrash, 5}, ¬

{88, purple, pMovePurpleMessagesToTrash, 4}, ¬

{81, red, pMoveRedMessagesToTrash, 3}, ¬

{75, orange, pMoveOrangeMessagesToTrash, 2}, ¬

{50, yellow, pMoveYellowMessagesToTrash, 1}, ¬

{0, 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 pUnflagSpamMessages then

set _message’s flag index to -1

end if

if pFlagSpamMessages then

set _message’s flag index to _flagColor

end if

return _moveToTrash

end if

end repeat

end tell

end colorMessageAndDecideIfShouldMoveToTrash

– Logging

on debugLog(_message)

if pEnableDebugLogging then my logToConsole(_message)

end debugLog

on logToConsole(_message)

set _logMessage to "SpamSieve [Apple Mail Rescue Good Messages] " & _message

do shell script "/usr/bin/logger -s " & _logMessage’s quoted form

end logToConsole

on makeLogMessage(_action, _mailbox, _detail)

return _action & " " & my describeMailbox(_mailbox) & ": " & _detail

end makeLogMessage

on describeMailbox(_mailbox)

tell application “Mail”

set _mailboxName to _mailbox’s name

try

set _accountName to name of _mailbox’s account

on error

set _accountName to “On My Mac”

end try

return ““” & _accountName & “” / “” & _mailboxName & “””

end tell

end describeMailbox

– Logging Helpers

on sourceFromMessage(_message)

tell application “Mail”

my debugLog(my makeLogMessage(“Getting source of message in”, _message’s mailbox, _message’s subject))

return _message’s source

end tell

end sourceFromMessage

I don’t want to move everything to the inbox (ugh) but if it works … I will see if the modified rescue script can be fixed, but otherwise I am going to try this.

I found it easy to modify his script. Good luck.

Are there new SpamSieve log entries for these after running the script?

You would change pMarkSpamMessagesRead from false to true.

Are the messages marked as read? The script only processes unread messages.

By “unmarked,” do you mean that there is no background color? Are these marked as read?

I am pretty sure the fact that the messages were read was the problem. This morning I ran the script and every last email in my junk box moved to trash. Thanks for helping me sort this out. I don’t know what caused junk mail to increase so much recently but I was tearing my hair out.

Eureka! (I think) I was about to double check how to properly create 5 new SpamSieve color rules in Apple Mail to move Blue, Gray, Purple, Red and Orange spam direct to the Trash to cope with the massive volume of spam my Apple email address is attracting. SS is catching them, but my Junk folder is full of garbage, hundreds a day.

If I edit this in the Rescue Good Messages script for the colors I want to trash

property pMoveBlueMessagesToTrash : true

I’ll skip the Junk folder for those colors?

If you want to move all those colors to the trash, you don’t need a separate rule for each color. You can put multiple colors in the same color rule name, e.g. SpamSieve [Blue] [Gray] [Purple] [Red] [Orange].

You don’t need the script at all for incoming messages that SpamSieve is already correctly processing. You can just use the [Score] and color rules to change where those colors go. The rescue script is for processing messages that are in Junk because of a server junk filter rather than SpamSieve.

Ah. Thanks for the multi-color rule tip.
The server Junk filter (iCloud) is all but useless. 99% of my Junk is there after being caught by SS. It is literally a chore to screen and empty due to the large daily volume. If I can auto-delete the color flagged SS caught as certain junk (after 10 years I trust it!) and trash it, I only have to review a few outliers.