Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Applications > Mail: fwd spam as attachment

Mail: fwd spam as attachment
Thread Tools
Dedicated MacNNer
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jun 21, 2004, 03:19 PM
 
Guys - I was wondering if anyone knows how to write an applescript for Mail that will allow me to forward incoming spam to our postmaster as an attachment - much like the "Forward message as an attachment?" feature of pine (and Mulberry) - which preserves header info etc.

This is the preferred way of forwarding spam to the postmaster.

Any ideas?

Cheers.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 22, 2004, 08:05 AM
 
Here's something to get you started:

[php]
tell application "Mail"
tell first account
tell mailbox named "inbox"
tell first message
set allHeaders to all headers
set messageContent to content
end tell
end tell
end tell
end tell

set totalText to allHeaders & messageContent

tell application "TextEdit"
activate
set newDoc to make new document at front
set the name of window 1 to "Spam"
set the text of newDoc to totalText
end tell
[/php]

Alas - This script just does the first message in the first account - I tried to get it to work on the selection - but Mail seems to have a problem with getting the selection.

Anyone have any idea why this won't work?

[php]
tell application "Mail"
get the selection
tell the selection
get properties
end tell
end tell
[/php]
tell application "Mail"
get selection
{message 12 of mailbox "INBOX" of account "Mac.com"}
get properties of selection
"Mail got an error: NSCannotCreateScriptCommandError"
You know it makes sense. ☼ ☼ ☼ Growl.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 22, 2004, 08:22 AM
 
latest try:

[php]
tell application "Mail"

if the selection is {} then
beep
return
end if


set the_selection to the selection
repeat with i from 1 to (count of the_selection)
set thisSelectedItem to (item i of the_selection)
if class of thisSelectedItem is message then
if (junk mail status of thisSelectedItem is true) then
tell thisSelectedItem
set allHeaders to all headers
set messageContent to content
set totalText to allHeaders & messageContent
-- my makeNewTextEditDoc(totalText, "Spam")

set this_file to (((path to desktop folder) as text) & "Spam.txt")
my write_to_file(totalText, this_file, false)

my attachFileToNewMessage(this_file, "postmaster@domain.com", "Hello World")

end tell
end if
end if
end repeat
end tell



to attachFileToNewMessage(fileToAttach, addressToSendTo, emailSubject)
tell application "Mail"
set the new_message to make new outgoing message at beginning of every outgoing message with properties {subject:emailSubject, visible:true}
tell new_message
-- make new recipient at front with properties {address:addressToSendTo, name:"Postmaster"}
-- make new attachment with properties {file name:fileToAttach}

make new attachment with properties {file name:fileToAttach} at before the first word of the first paragraph
end tell
activate
end tell

end attachFileToNewMessage



to write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file

[/php]
(Last edited by Diggory Laycock; Jun 22, 2004 at 08:56 AM. )
You know it makes sense. ☼ ☼ ☼ Growl.
     
m.brown  (op)
Dedicated MacNNer
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jun 22, 2004, 10:23 AM
 
Originally posted by Diggory Laycock:
[B]latest try:
Cheers for this. I have tried the code however it only opens a new mail message.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 22, 2004, 01:09 PM
 
Yes - I got stuck on the attachment part (can't get it to attach the text file to the new message) - will have another look.

[edit]

try this:

[php]
set this_file to (((path to desktop folder) as text) & "Spam.txt")
set spamMessageSubject to "Spam forward"
set postmasterAddress to "postmaster@domain.com"

tell application "Mail"

if the selection is {} then
display dialog "No messages selected in Mail." buttons {"OK"} default button 1
beep
return
end if

set the_selection to the selection
repeat with i from 1 to (count of the_selection)
set thisSelectedItem to (item i of the_selection)
if class of thisSelectedItem is message then
if (junk mail status of thisSelectedItem is true) then
tell thisSelectedItem
my write_to_file(source, this_file, false)
my attachFileToNewMessage(this_file, postmasterAddress, spamMessageSubject)
end tell
end if
end if
end repeat

end tell



to attachFileToNewMessage(fileToAttach, addressToSendTo, emailSubject)
tell application "Mail"
set the new_message to make new outgoing message at beginning of every outgoing message with properties {subject:emailSubject, visible:true}
tell new_message
make new to recipient at end of to recipients with properties {address:addressToSendTo}
make new attachment with properties {file name:POSIX path of fileToAttach} at before the first character
end tell
activate
end tell
end attachFileToNewMessage


to write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
[/php]
(Last edited by Diggory Laycock; Jun 22, 2004 at 01:35 PM. )
You know it makes sense. ☼ ☼ ☼ Growl.
     
m.brown  (op)
Dedicated MacNNer
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jun 22, 2004, 02:19 PM
 
Originally posted by Diggory Laycock:
[B]Yes - I got stuck on the attachment part (can't get it to attach the text file to the new message) - will have another look.
Wow! Thank you so much - that is brilliant. Can I have permission to make this available to other users?

Cheers,

Mike
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 22, 2004, 02:31 PM
 
Originally posted by m.brown:
Wow! Thank you so much - that is brilliant. Can I have permission to make this available to other users?

Cheers,

Mike
Sure.
You know it makes sense. ☼ ☼ ☼ Growl.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 22, 2004, 03:52 PM
 
Oh yes, if you want to be able to invoke the script with a Control-key see this hint:

http://www.macosxhints.com/article.p...40618161426854
You know it makes sense. ☼ ☼ ☼ Growl.
     
m.brown  (op)
Dedicated MacNNer
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jun 23, 2004, 05:25 AM
 
Originally posted by Diggory Laycock:
Oh yes, if you want to be able to invoke the script with a Control-key see this hint:

http://www.macosxhints.com/article.p...40618161426854
One thing I just noticed in the composed message in the to: field <postmaster@domain.com> appears twice:

<postmaster@domain.com><postmaster@domain.com>

Do you know what's causing this?

One other thing (sorry to be cheeky!) is it possible for the Spam.txt that appears on the desktop to be moved to the trash - or wouldn't you recommend this?

Cheers!
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jun 23, 2004, 05:44 AM
 
Originally posted by m.brown:
One thing I just noticed in the composed message in the to: field <postmaster@domain.com> appears twice:

<postmaster@domain.com><postmaster@domain.com>

Do you know what's causing this?
Mail seems to show the email address twice if it hasn't been given a "proper" name - you can change this by altering the line:

[php]make new to recipient at end of to recipients with properties {address:addressToSendTo} [/php]

to

[php]make new to recipient at end of to recipients with properties {address:addressToSendTo , name: "Postmaster"} [/php]

or whatever you want to call him.


One other thing (sorry to be cheeky!) is it possible for the Spam.txt that appears on the desktop to be moved to the trash - or wouldn't you recommend this?

Cheers!
I didn't do this because I wasn't sure if mail could deal with the attachment being deleted before mail delivery - but I've just checked and it makes it's own copies of attachments - so it can be deleted without problems.

add a new line below this line:

[php]my attachFileToNewMessage(this_file, postmasterAddress, spamMessageSubject)
[/php]

so that it becomes:

[php]
my attachFileToNewMessage(this_file, postmasterAddress, spamMessageSubject)
tell application "Finder" to move item this_file to trash
[/php]
You know it makes sense. ☼ ☼ ☼ Growl.
     
m.brown  (op)
Dedicated MacNNer
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jun 23, 2004, 05:52 AM
 
Originally posted by Diggory Laycock:
SNIP
Thanks so much - this is a great solution.

Mike
     
m.brown  (op)
Dedicated MacNNer
Join Date: Jan 2002
Status: Offline
Reply With Quote
Jun 23, 2004, 10:05 AM
 
SNIP
I've been testing the script and the postmaster reports that all the carriage returns & line feeds look screwed up on a unix system - indeed if you post the text into pine each character is placed on a new line.

If you open the file in SubEthaEdit then behind each character you get a box with a ? in it.

Any ideas what is happening here?
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:32 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2