I hope I haven’t missed this, but I can’t seem to find an answer anywhere online.
I created a script to handle purging my Junk mail. It not only deletes the messages, but sends them off as attachments to be learned by my mail server’s spam filter.
The problem is that I don’t want to save this message in my sent folder. Is there any way to send a message through mail that doesn’t store it in the Sent folder? (Otherwise, I guess I’ll have to write some kind of command-line utility to process the junk.)
set spamAccount to "spam@example.com"
set tempFolderName to "JunkMail"
set tempFolderPath to path to home folder
-- Create temporary folder for junk
tell application "Finder"
if (exists folder (tempFolderPath & tempFolderName as string)) = false 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
set newMessage to make new outgoing message with properties {subject:"spam report", 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
set sourceFile to ((tempFolderPath & tempFolderName as string) & ":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
-- Problem here: How to send without saving to Sent?
send newMessage
-- delete junk messages
repeat with junkMessage in junkMessages
set deleted status of junkMessage to true
end repeat
end if
-- turn sounds back on
set should play other mail sounds to true
end tell
-- Delete temporary folder
tell application "Finder"
do shell script ("rm -Rf \"~/" & tempFolderName & "\"")
end tell
or just add a line to your script to delete the message immediately after sending, or a line to trigger a rule which does the same thing.
I’m not entirely sure what you mean. The sent message appears to be a separate copy from the outgoing message, so there’s no way to delete the message (deleting the outgoing message apparently doesn’t work.) I tried searching for and deleting the message from Sent, using a randomly generated id, but that appeared to take way too long. Also, I couldn’t figure out how to use Spotlight to find the sent message.
I hadn’t thought about adding a rule, I’ll look into that. ” Ok, no go on the rule. I don’t know how you create a rule for the “Sent” mailbox, since Mail ignores the rule when it adds a message to Sent.
Thank you for your suggestions, though!
Any other ideas?
Update - Absolutely simple solution:
I didn’t realize that you could qualify a message selection, so I ended up with:
set sentMessages to every message in sent mailbox where subject is messageSubject
repeat with sentMessage in sentMessages
set deleted status of sentMessage to true
end repeat
which runs after the message is sent.
If anyone is interested in the final script, I’ll happily post it.
I would love to see the final version - thanks!
Here goes - it’s rather long:
set spamAccount to "spam@example.com"
set tempFolderName to "JunkMail"
set tempFolderPath to path to home folder
set maxSendAttempts to 5
set maxSentDeleteAttempts to 10
-- from here onward should not need to be modified
set tempFolder to (tempFolderPath as string) & tempFolderName
-- Create 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
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
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
-- Try 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
-- pause for one second to wait for the sent message to show up
delay 1
-- delete sent message
-- 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
-- delete junk messages
repeat with junkMessage in junkMessages
set deleted status of junkMessage to true
end repeat
end if
-- turn sounds back on
set should play other mail sounds to true
end tell
-- Delete temporary folder
set posixJunkFolder to POSIX path of (tempFolder)
set deleteCommand to "rm -Rf " & quoted form of posixJunkFolder
do shell script (deleteCommand)
Save this as Empty Junk Mail in the Mail Scripts folder. Edit the email address to be your spam-learning account, and enjoy. It’s been working well for me for about 7-8 clearings since my last update.
Note: this will turn off your sounds if it fails for some reason. To turn them back on, simply re-run the script, even if the Junk Mail folder is empty. Re-running it will also delete the temporary junk folder created in Finder if it fails.