Jump to content
svv07

Applescript: SendToSpamCop

Recommended Posts

Below is the code from my first steps in to the applescript world. I was unsatisfied with the results produced by other scripts mentioned on this forum so I tried to make something that suited my needs better. I'm posting it here so you can try my scri_pt, comment on it, or adapt it to your needs.

General description:

SendToSpamCop for Mail.app takes the source-code of the selected mails and places it in a SpamCop-readable files in a folder on the users desktop. It then generates a mail with the forwarding address of the users SpamCop account as recipient. The scri_pt also attaches the generated files to this mail.

I deliberately haven't added functionality to auto send the mail to SpamCop or to delete the generated SpamCop-folder on the desktop. Though this is not that hard to implement, I thought this would be better for both debugging purposes and for users who want to do something else with the generated files.

Though the scri_pt can be run as it is, you may want to change the value of variable SCaccount in the code, before using it. How to do this is described in the first comments of the scripts source-code. If you don't feel like editing this variable the scri_pt would still work but you have to manually type your SpamCop forwarding address in the recipients field of the generated email.

The scri_pt is open source so do with it what you like. But I would really appreciate it if you can leave some credits to the original author (me B) ).

This is my first applescript coding attempt so comments and suggestions are appreciated.

Here is the code:

(* SendToSpamCop V1.0
  written by S.J.L. v/d Velden
  The code below is open source and you may freely  edit and redistribute it.
  Though I would appreciate it if you gave credits to  the original author (me).
  --------------------------------------------------------

   You have to customize a variable to make this scri_pt
   work with your SpamCop-account.

  Below you see the line that begins with "set
  SCaccount..."
  Replace the text YOURACCOUNT with the email address
  you've got from SpamCop.net to forward your spam to.
   *)
--

set SCaccount to "YOURACCOUNT"


(* Below is the rest of the sourcecode of the scri_pt.
   Please be very carefull editing this code *)

-- Create a SpamCop folder on the desktop
set theOutputFolderPath to path to desktop folder
set theNewFolderName to "SpamCop"

tell application "Finder"
	if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then
		make new folder at desktop with properties {name:theNewFolderName}
	end if
end tell

-- Create a new message in mail addressed to the users SpamCop account. Read the source from the selected messages in mail and save it as SpamCop readable file in the newly created SpamCop folder. Then attach each file to the new message.
tell application "Mail"
	set theMessages to the selection
	set counter to 1
	set theMessage to (make new outgoing message with properties {visible:true, subject:"report spam", content:" "})
	repeat with thisMessage in theMessages
		set sourceFile to ((theOutputFolderPath & theNewFolderName as string) & ":ml" & counter & ".src")
		set thisSource to the source of thisMessage 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 theMessage
			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
	tell theMessage
		make new to recipient at end of to recipients with properties {address:SCaccount}
	end tell
end tell

Share this post


Link to post
Share on other sites

svv07,

Thanks for your efforts here. I'd been wishing for a solution to make attachments like that.

I'll play around with it some more and offer any suggestions if/when I have something useful.

--

Mike

[Edited to remove an erroneous result I only *thought* I saw. :) ]

Edited by tmneast

Share this post


Link to post
Share on other sites
Below is the code from my first steps in to the applescript world. I was unsatisfied with the results produced by other scripts mentioned on this forum so I tried to make something that suited my needs better. I'm posting it here so you can try my scri_pt, comment on it, or adapt it to your needs.

Helpful Info for Newbies

This scri_pt appears to still work fine as of Mac OS X 10.5.6, with Mail 3.5.

To Install:

1. Copy the code provided.

2. Open "scri_pt Editor.app" on your HD. It should be located 'HD/Applications/Applescript/scri_pt Editor.app'.

3. Paste it into the top half of the window.

4. Make any necessary changes as detailed above (Your Spamcop reporting email, etc.)

5. Hit the "Compile" button.

6. Pull down 'File>Save as', file format should be 'scri_pt', do not 'hide extension' (good practice).

7. Save into 'HD/Library/Scripts/Mail Scripts/' (good practice).

Initial Setup To Use: (my method)

1. Open "ApplescriptUtility.app" on your HD. It should be located 'HD/Applications/Applescript/ApplescriptUtility.app'.

2. 'Check' the box 'show scri_pt menu in menu bar'.

3. Quit

*This should add the 'scri_pt' icon/menu to your menu bar. You will not need to do this again unless you accidentally remove the icon/menu.*

To Use In Mail: (my method)

1. Select spam message(s).

2. Pull down the 'scri_pt' menu/icon in the menubar, and select your scri_pt.

3. Once it's done running, send.

Share this post


Link to post
Share on other sites
Helpful Info for Newbies
Thank you for the dusting-off and the installation notes. I may say that this scri_pt was a huge timesaver for me last year when I was targeted by hundreds of blowback bounces due to my address having been forged by a spammer. I was able to submit all these mails very efficiently for reporting.

-- rick

Share this post


Link to post
Share on other sites

Hey @svv07 & Co.

I took your AppleScript code a little further. Here's what I did:

  1. Because I check my Mail regularly on an iPad or iPhone where AppleScript is not available, I switched the scri_pt from fetching e-mail messages from Mail's spam folder. Therefore you can safely mark your messages as spam and process it, as soon as you returned to your Mac.
  2. For the scri_pt to not report already reported spam, it won't process any messages in your spam folder, that matches one of these criteria:
    • The message's subject starts with `[spam]`.
    • The message is flagged. (See below.)
  3. I moved the inline code to set the personalised SpamCop e-mail address to report spam (Reporting Address) to a property..
  4. I introduced some cleverness to set the Reporting Address even when the scri_pt is run as an AppleScript App (Applet). It will ask for your SpamCop Reporting Address on its first launch automatically.
    • If you run my scri_pt as an Applet, note that you can reset the Reporting Address by holding down the option key while starting up the Applet.
  5. My scri_pt is capable to automatically send the reporting e-mail after it fetched all spam.
  6. For the scri_pt to not report spam twice, it flags all processed messages in your spam folder.

So, folks, attached is the code and the said Applet; either download and run the Applet directly or open the source code file with scri_pt Editor on your Mac and save it as an Application to your Desktop or wherever you like. 

Have fun – and keep reporting spam,
yours sincerely,
Moritz

 

Report to SpamCop.zip

Share this post


Link to post
Share on other sites

Hi folks

@Lking kindly informed me, that attachments or links to downloads aren't allowed in this forum. So here comes the source code; feel free to ask on how to compile it into an app:

(*
	Report to SpamCop AppleScript
	by Moritz Zimmer
	
	based on SendToSpamCop V1.0
   written by S.J.L. v/d Velden
	
	NOTA BENE: Hold down the Option key to reset your 
	SpamCop e-mail address saved earlier.
   *)

property SpamCopAddress : ""

on run
	
	if number of characters in SpamCopAddress is less than 1 or option of (getModifierKeys()) is true then
		set dialogResult to (display dialog "Enter the SpamCop e-mail address associated with your account." default answer "" buttons {"Cancel", "Save", "Save and Continue"} default button 3 cancel button 1 with title "SpamCop AppleScript" with icon caution)
		if button returned of dialogResult is not "Cancel" then
			if button returned of dialogResult is "Save and Continue" then
				set SpamCopAddress to (text returned of dialogResult)
				set SCaccount to SpamCopAddress
			else
				set SCaccount to (text returned of dialogResult)
			end if
		else
			return
		end if
	else
		set SCaccount to SpamCopAddress
	end if
	
	(* Below is the rest of the sourcecode of the scri_pt.
   Please be very carefull editing this code *)
	
	-- Create a SpamCop folder on the desktop
	set theOutputFolderPath to path to desktop folder
	set theNewFolderName to "SpamCop"
	
	tell application "Finder"
		if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then
			make new folder at desktop with properties {name:theNewFolderName}
		end if
	end tell
	
	-- Create a new message in mail addressed to the users SpamCop account. Read the source from the selected messages in mail and save it as SpamCop readable file in the newly created SpamCop folder. Then attach each file to the new message.
	tell application "Mail"
		
		set theMessages to (messages of junk mailbox whose flagged status is false)
		set counter to 0
		
		if number of items in theMessages is greater than 0 then
			
			set theMessage to (make new outgoing message with properties {visible:true, subject:"spam Report through AppleScript", content:" "})
			repeat with thisMessage in theMessages
				
				if subject of thisMessage does not start with "[spam]" then
					set thisDate to date received of thisMessage
					if thisDate > ((current date) - (40 * hours)) then
						set counter to counter + 1
						set sourceFile to ((theOutputFolderPath & theNewFolderName as string) & ":ml" & counter & ".src")
						set thisSource to the source of thisMessage 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 theMessage
							tell content
								make new attachment with properties {file name:theAttachment} at before the first character
							end tell
						end tell
						
					end if
					
					set flagged status of thisMessage to true
					set read status of thisMessage to true
				else
					set flagged status of thisMessage to true
					set read status of thisMessage to true
				end if
				
			end repeat
			
		end if
		
		if counter is greater than 0 then
			tell theMessage
				make new to recipient at end of to recipients with properties {address:SCaccount}
			end tell
		end if
		
	end tell
	
	delay 3
	
	if counter is greater than 0 then
		set myAnswer to display dialog ("Would you like to report " & counter & " messages to SpamCop?") giving up after 90
		if gave up of myAnswer is true or button returned of myAnswer is "OK" then
			tell application "Mail"
				tell theMessage
					send
				end tell
			end tell
		end if
	end if
	
end run

use framework "Cocoa"
on getModifierKeys()
	set modifierKeysDOWN to {command:false, option:false, control:false, shift:false, fn:false, capslock:false}
	set modifierBits to current application's NSEvent's |modifierFlags|()
	set modifierBits to modifierBits * 1
	if (modifierBits > 0) then
		if (modifierBits > 8388607) then
			-- fn key is pressed, subtract it away
			set modifierBits to modifierBits - 8388608
			set fn of modifierKeysDOWN to true
		end if
		if (modifierBits > 1048575) then
			-- command key is pressed, subtract it away
			set modifierBits to modifierBits - 1048576
			set command of modifierKeysDOWN to true
		end if
		if (modifierBits > 524287) then
			-- option key is pressed, subtract it away
			set modifierBits to modifierBits - 524288
			set option of modifierKeysDOWN to true
		end if
		if (modifierBits > 262143) then
			-- ctrl key is pressed, subtract it away
			set modifierBits to modifierBits - 262144
			set control of modifierKeysDOWN to true
		end if
		if (modifierBits > 131071) then
			-- shift key is pressed, subtract it away
			set modifierBits to modifierBits - 131072
			set shift of modifierKeysDOWN to true
		end if
		if (modifierBits > 65535) then
			-- capslock key is pressed, subtract it away
			set modifierBits to modifierBits - 65536
			set capslock of modifierKeysDOWN to true
		end if
	end if
	return modifierKeysDOWN
end getModifierKeys

Yours sincerely,
Moritz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×