Skip to content

Instantly share code, notes, and snippets.

@sentient06
Created February 25, 2014 13:23
Show Gist options
  • Save sentient06/9208633 to your computer and use it in GitHub Desktop.
Save sentient06/9208633 to your computer and use it in GitHub Desktop.
iCloud Junk Mail Redirection
on run {input, parameters}
set spamAccount to "[email protected]"
set tempFolderName to "JunkMail"
set tempFolderPath to path to home folder
set maxSendAttempts to 5
set maxSentDeleteAttempts to 10
set deleteSentMessage to false
set deleteJunkMessage to false
-- Test behaviour by observing the home directory and the drafts box in Mail
set testingBehaviour to true
-- From here onward should not need to be modified
set tempFolder to (tempFolderPath as string) & tempFolderName
-- Creates temporary folder for junk
tell application "Finder"
if not (exists folder tempFolder) then
make new folder at tempFolderPath with properties {name:tempFolderName}
end if
end tell
tell application "Mail"
set junkMessages to every message of junk mailbox
set messageCount to (count of junkMessages) as number
if (messageCount is greater than 0) then
-- Sets uniqueID to "spam report " & (round (random number from 10000 to 99999) rounding down as string)
set messageSubject to "spam report - delete now"
set newMessage to make new outgoing message with properties {subject:messageSubject, content:" "}
tell newMessage
make new to recipient at end of to recipients with properties {address:spamAccount}
end tell
set counter to 1
repeat with junkMessage in junkMessages
-- Marks as Junk for Mail
set junk mail status of junkMessage to true
set sourceFile to (tempFolder & ":ml" & counter & ".eml")
set thisSource to the source of junkMessage as string
set f to open for access sourceFile with write permission
set eof of f to 0
write thisSource to f
close access f
tell "Finder"
set theAttachment to sourceFile as alias
end tell
tell the newMessage
tell content
make new attachment with properties {file name:theAttachment} at before the first character
end tell
end tell
set counter to counter + 1
end repeat
set should play other mail sounds to false
if testingBehaviour is false then
-- Tries to send the message up to 5 times
set attemptCount to 0
repeat until attemptCount is maxSendAttempts
set sendResult to send newMessage
if (sendResult) then
exit repeat
else
set attemptCount to attemptCount + 1
end if
end repeat
if attemptCount is greater than or equal to maxSendAttempts then
display alert "Unable to send message." as critical
return
end if
display alert "Spam redirected" message messageCount & " messages were redirected to Apple!" as informational
end if
-- Pauses for one second to wait for the sent message to show up
delay 1
-- Deletes sent message
if deleteSentMessage is true then
-- Tries to delete the message over 5 seconds, at one second intervals
set tryToDelete to 0
repeat while tryToDelete is less than maxSentDeleteAttempts
set sentMessages to every message in sent mailbox where subject is messageSubject
if (count of sentMessages) is greater than 0 then
repeat with sentMessage in sentMessages
set deleted status of sentMessage to true
end repeat
-- stop checking
exit repeat
else
-- increment deletion counter
set tryToDelete to tryToDelete + 1
-- Pause for one second
delay 1
end if
end repeat
if tryToDelete is greater than or equal to maxSentDeleteAttempts then
display alert "Unable to delete sent message." as warning
end if
end if
-- Deletes junk messages
if deleteJunkMessage is true then
repeat with junkMessage in junkMessages
set deleted status of junkMessage to true
end repeat
end if
end if
-- Turns sounds back on
set should play other mail sounds to true
end tell
-- Deletes temporary folder
set posixJunkFolder to POSIX path of (tempFolder)
set deleteCommand to "rm -Rf " & quoted form of posixJunkFolder
do shell script (deleteCommand)
end run
@sentient06
Copy link
Author

This is a small Apple Script to redirect all current junk mail to Apple. Partially tested on Mavericks, Mail v. 7.1.
Clean-paste code into new workflow (run applescript action) to have it working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment