Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript for Junk Mail

Hi,


I'm trying to use a combination of rules and Applescript to report the mail that the Apple's mail program identifies as junk to two different addresses and then to send it as an attachment to a third address, but I just can't get it to work.


If I could move it to another folder when done, that would be even slicker.


Before I share my script, I'm having some puzzling problems as well.


1) Working in Mavericks 10.9.5, over time, the actions in my rules seem to crowd together so I can no longer work on them. One action overlaps another. If I quit and come back in, sometimes that rescues the GUI, but other times, I just have to start all over. Looks something like this:


User uploaded file

2) Even when I tell mail to run the Applescript first, then move the mail to another folder, it reorders my actions to make the move first. So I can't tell it to run an Applescript, THEN move the mail.

3) That said, this shouldn't pose a problem since Apple explicitly states:

User uploaded file

I'm not telling it to stop evaluating, and the move isn't being orchestrated by rules on a server or a synced device, so why does it fail to run the script?


With that in mind, I've tried a few different versions of this script, but here is my latest. I'm pretty close, but I can't find a way to set the messages equal to new junk that shows up in my inbox. Also, it sometimes seems to send the same message twice. Again, if there was some additional code that would allow me to send the mail as an attachment to yet a third party, that would be particularly helpful.


Rule 1:

If message is Junk Mail,

Run Applescript:


set addressList to {"address1@domain1.net", "address2@domain2.net"}

using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule

tell application "Mail"

repeat with eachMessage in theMessages

set newMessage to makenewoutgoing messageat end of outgoing messages

tell newMessage

set content to (eachMessage's source as Unicode text)

set subject to "Fwd SPAM: " & eachMessage's subject

repeatwithifrom 1 tocountaddressList

makenewto recipientat end of to recipientswith properties {address:itemi of addressList}

end repeat

endtell

send newMessage

end repeat

end tell

end perform mail action with messages

end using terms from




What I'm particularly uncertain about is the early part of the code. I've basically cut and pasted this together, and I think the first part is designed to select any message that has been handled by a rule action first, which seemed like a good way to target incoming mail that's been flagged as junk in theory, but that doesn't seem to be working. I've even tried performing some simple action to the junk mail (e.g. play sound) first to "prime" it for the script, but no luck. I think what I need is the proper code for setting my message selection to new mail that's been flagged as junk, if that makes any sense.

I did come across some info on sending mail as an attachment, but it seemed far beyond my pay grade. Here is a link to the discussion however.

Thanks in advance to anyone who can help.

Use Applescript or Automator to forward junk mail as attachment to spam@icloud.com

iMac (27-inch, Late 2013), OS X Mavericks (10.9.5)

Posted on Oct 5, 2015 8:39 PM

Reply

There are no replies.

Applescript for Junk Mail

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.