pit_jj72

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

  1. Open the message and choose “Forward as Attachment” from the Message menu.
  2. Forward the message to iCloud spam@icloud.com.
  3. 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

Close

Q: How to send a mail as an attachment in a mail with AppleScript?

  • All replies
  • Helpful answers

  • by Camelot,Solvedanswer

    Camelot Camelot Mar 24, 2014 2:32 PM in response to pit_jj72
    Level 8 (47,290 points)
    Mac OS X
    Mar 24, 2014 2:32 PM in response to pit_jj72

    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.

  • by pit_jj72,

    pit_jj72 pit_jj72 Mar 25, 2014 12:16 PM in response to Camelot
    Level 1 (1 points)
    Mar 25, 2014 12:16 PM in response to Camelot

    Perfect!

    You clearly give me the way. Actually I have used the AppleScript Language Guide from https://developer.apple.com to finalize my AppleScript.

     

    You just need to select the spam message(s) in "Mail" application, then run the script.

     

    As I mentionned previously, it just do this automatically :

    OS X Mail

    1. Open the message and choose “Forward as Attachment” from the Message menu.
    2. Forward the message to iCloud spam@icloud.com.
    3. 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.

     

    Here is the entire working script :

     

    -- Ce script permet de lutter contre le spam.

    -- Je me suis appuyé sur les conseils sur http://support.apple.com/kb/TS4019?viewlocale=fr_FR

     

     

    tell application "Mail"

     

              set theMessages to the selection

              repeat with theMessage in theMessages

     

      -- enregistre le message dans un fichier pour le mettre en PJ ensuite

                        set msgContent to source of theMessage

                        set tmpFile to ((path to desktop) as rich text) & "ForwardedMessage"

                        set referenceNumber to open for access tmpFile with write permission

      write msgContent to referenceNumber

      close access referenceNumber

     

      -- envoi le message sélectionné en PJ à spam@icloud.com

                        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 newMessage

      make new attachment with properties {file name:tmpFile} at after last paragraph

                                  end tell

                        end tell

      send newMessage

     

      -- envoi le message sélectionné en PJ à abuse@<domaine du spammeur>

                        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 theSpamAddress to text item 2 of theSpamAddress

                                  set AppleScript's text item delimiters to {">"}

                                  set spamerDomainName to text item 1 of theSpamAddress

                                  set theNewSpamAddress to "abuse@" & spamerDomainName

     

      make new to recipient with properties {address:theNewSpamAddress}

                                  tell content of newMessage

      make new attachment with properties {file name:tmpFile} at after last paragraph

                                  end tell

                        end tell

      send newMessage

     

     

              end repeat

     

    end tell