Q: How to send a mail as an attachment in a mail with AppleScript?
Hi,
I want to manage my spam using the advices found in support.apple website :
OS X Mail
- Open the message and choose “Forward as Attachment” from the Message menu.
- Forward the message to iCloud spam@icloud.com.
- Forward the message again to abuse@domain, replacing domain with the part of the sender's email address after the @ symbol. For example, if the sender's email address is spammer@spammydomain.com, forward the message to abuse@spammydomain.com.
I don't know how to make a message as an attachment...
set theAttachment to theMessage
...
make new attachment with properties {file name:theAttachment} at after last paragraph
Here is my whole script, if it runs, it could be useful :
tell application "Mail"
-- envoi les messages sélectionnés en PJ à spam@icloud.com
set theMessages to the selection
repeat with theMessage in theMessages
set theAttachment to theMessage
set newMessage to make new outgoing message at end of outgoing messages
tell newMessage
set subject to "Fwd: " & theMessage's subject
make new to recipient with properties {address:"spam@icloud.com"}
tell content of theMessage
make new attachment with properties {file name:theAttachment} at after last paragraph
end tell
end tell
send newMessage
end repeat
-- envoi les messages sélectionnés en PJ à abuse@<domaine du spammeur>
set theMessages to the selection
repeat with theMessage in theMessages
set theAttachment to theMessage
set newMessage to make new outgoing message at end of outgoing messages
tell newMessage
set subject to "Fwd: " & theMessage's subject
set theSpamAddress to theMessage's sender
set AppleScript's text item delimiters to {"@"}
set spamerDomainName to text item 2 of theSpamAddress
set theNewSpamAddress to "abuse@" & spamerDomainName
make new to recipient with properties {address:theNewSpamAddress}
tell content of theMessage
make new attachment with properties {file name:theAttachment} at after last paragraph
end tell
end tell
send newMessage
end repeat
end tell
Thank you!
Posted on Mar 24, 2014 12:43 PM
I don't see any way to directly invoke the 'Forward as Attachment' option (other than UI events, which I prefer to avoid). However, it seems that all this option does is create a file that contains the message text (including headers) and attaches that to a new outgoing message, so that shouldn't be hard to replicate.
It's easy to get the source content of any given message:
tell application "Mail"
-- code to select any given message
set MsgContent to source of theMessage
end tell
Once you have the source, it's easy to write this to a file:
set tmpFile to (open for access file ((path to desktop as text) & "Forwarded Message") with write permission)
write msgContent to tmpFile
close access tmpFile
Now you have a file you can attach to a new email. Once you've sent it you can delete the file.
Posted on Mar 24, 2014 2:32 PM