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?
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?
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
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!
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