Forwarding and email as an attachment using Applescript

Figured it out…

Pretty much works like all the other “attachment” code out there, but the trick to getting the MIME part to be text/RFC822 is to use the “eml” extension on the temporary file that is used for the attachment.

set sourceFile to "Macintosh HD:forward.eml"
tell application "Mail"
	set theMessages to the selection
	repeat with thisMessage in theMessages
		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
		
		set newMessage to make new outgoing message at end of outgoing messages
		tell newMessage
			set subject to thisMessage's subject
			make new to recipient with properties {name:"John Doe", address:"jdoe@domain.com"}
			tell content to make new attachment with properties {file name:sourceFile as alias} at after the last paragraph
		end tell
		
		set visible of newMessage to true
		--send newMessage
	end repeat
end tell
do shell script "rm '/forward.eml'"

This is exactly what I have been looking for - thanks! I want to take each Spam message that I get and send it spam@uce.gov and spamcop, and then delete the spam from the Spam folder.

Forgive my ignorance, but how would you accomplish these additional steps - I see that you have a placeholder for the send message step, but how is that actually done, and how do I delete the original spam message?

And as an additional refinement, how would I automatically select all messages in the Spam folder?

Your help is greatly appreciated.
Cheers,
Allan

Two years of blissful operation and now, after upgrading to Lion, the script fails on the statement:

with a “Mail got an error: Network file permission error.”

Can anyone shed any light on this? Any help will be appreciated…

Cheers,
Allan

Model: MacBook Pro
AppleScript: 2.4.1
Browser: Safari 535.1
Operating System: Mac OS X (10.7)

Hi,

two suggestions:

¢ Add the keyword file, because sourceFile is just a literal string


set f to open for access file sourceFile with write permission

¢ put the open/write/close lines out of the Mail tell block by using a handler

Thanks for the quick reply, Stefan, but I am going to show my ignorance (which can be fixed). What is a handler, and if I take it out of the Tell, how to I keep adding email messages to the file?

Perhaps I could twist your arm to provide a code snippet?

Cheers,
Allan


property sourceFile : "Macintosh HD:forward.eml"

tell application "Mail"
	set theMessages to the selection
	repeat with thisMessage in theMessages
		set thisSource to the source of thisMessage as string
		my writeToFile(thisSource)
		set newMessage to make new outgoing message at end of outgoing messages
		tell newMessage
			set subject to thisMessage's subject
			make new to recipient with properties {name:"John Doe", address:"jdoe@domain.com"}
			tell content to make new attachment with properties {file name:sourceFile as alias} at after the last paragraph
		end tell
		
		set visible of newMessage to true
		--send newMessage
	end repeat
end tell
do shell script "rm '/forward.eml'"


on writeToFile(theText)
	set f to open for access file sourceFile with write permission
	set eof of f to 0
	write theText to f
	close access f
end writeToFile

Thanks much! I learned something new: handler = subroutine?

Cheers,
Allan

I spoke too soon - getting the same error as before. See screenshot here:

http://dl.dropbox.com/u/3492119/Screenshots/Screen%20Shot%202011-10-17%20at%202.07.39%20PM.jpg

I think I solved my own problem: a user does not have write permission to the root of the hard drive, so Snow:forward.eml could not be created. I changed the path of the temporary file to “users:allan:downloads:forward.eml” and it works!

My older version does, too.

Thanks for the help, anyway.

Cheers,
Allan

This one works for me - be careful, it DELETES the target message after sending


set rA to "Spam@uce.gov" & ", " & "spamreport@cox.net" --put your own addressees here
tell application "Mail"
	set msg1 to item 1 of (get selection)
	set fullmsg to source of msg1
	set subj to subject of msg1
	set newmsgtask to make new outgoing message
	tell newmsgtask
		make new to recipient at end of to recipients ¬
			with properties {address:rA}
		--with properties {name:rN, address:rA}
		set visible to true
		set content to fullmsg
		set subject to "FWD: " & subj
		send
	end tell
	delete msg1
end tell

I would like to know how to restrict it to the Junk folder, looping to handle all messages therein