Previous 1 2 Next 27 Replies Latest reply: Jul 2, 2014 3:46 AM by Sean Dale1 Branched to a new discussion.
Sean Dale1 Level 5 Level 5 (4,145 points)

I recently moved the email out of Entourage and into Apple Mail for a company I do business for. I created a rule for them which does not run automatically, see my other post <a href="https://discussions.apple.com/thread/3300257">here</a>. Having experimented with variations on the rule and searched for answers on the web I have come to the conclusion that I may not be able to get the rule to run automatically. I found various suggestions on the web for using Terminal to automatically BCC sent emails but the downside is that it appears that you then can't send BCC emails to anyone else without using Terminal to disable this, which is not an option.

 

The best alternative would appear to be an AppleScript but I can't find anything suitable. Can anyone suggest a script that can be added to an Apple Mail rule so that all emails sent from 'whoever@companydomain.com' are automatically forwarded to 'office@companydomain.com'

  • twtwtw Level 5 Level 5 (4,900 points)

    Mail rules don't (and have never) worked for outgoing messages.  if you want to have an applescript that creates and sends an outgoing message with the appropriate recipients, that's easy enough.  On the other hand, will allow you to automatically bcc: yourself, so you could set up your system so that people bcc: themselves and then apple a rule action to the incoming bcc: copy.

  • Sean Dale1 Level 5 Level 5 (4,145 points)

    "Mail rules don't (and have never) worked for outgoing messages."

     

    That saves me from wasting any more time searching for a solution to that, thank you.

     

    "if you want to have an applescript that creates and sends an outgoing message with the appropriate recipients, that's easy enough."

     

    Yes please. My AppleScripting is not up to the task.

  • twtwtw Level 5 Level 5 (4,900 points)

    the basic script would be this. 

     

    set bccRecipients to {"email1@somewhere.com", "email2@somewhere.com"}

     

    tell application "Mail"

              set newMess to make new outgoing message at end of outgoing messages with properties {visible:true}

              tell newMess

                        repeat with thisRecipient in bccRecipients as list

      make new bcc recipient at end of bcc recipients with properties {address:thisRecipient}

                        end repeat

              end tell

    end tell

     

    running it from the script menu would probably be easiest.

  • Camelot Level 8 Level 8 (46,195 points)

    :: confused ::

     

    What's wrong with Mail -> Preferences -> Composing -> Automatically [CC/BCC] myself

     

    Doesn't that do precisely what you're attempting? No scripting required.

  • twtwtw Level 5 Level 5 (4,900 points)

    The OP doesn't want to automativally BCC himself, he wants to automatically BCC a third party or two.  I'm surprised Mail doesn't have a setting for this, but so far as I can tell it doesn't.

     

    Now if it was a strict corporate email, then Address book could be set up with a group list that would BCC everyone.  but I don't see any way to have that happen with an email from an individual.

  • Sean Dale1 Level 5 Level 5 (4,145 points)

    As twtwtw says what I need to do is get a copy of all outgoing emails from any given worker on the road BCC'd to their manager in the office.

     

    If you look in Mail -> Preferences -> Rules it appears that the option is there but the rule will not run automatically for some reason. The rule appears to be correctly set up as it will work as required if applied manually .... to be honest I can't see why this should be so difficult as it must be a fairly basic requirement for some people.

     

    If it would work as it should I wouldn't even be considering scripting as I would prefer to keep the whole thing as simple as possible but I am being forced in to looking at workaraounds

  • Sean Dale1 Level 5 Level 5 (4,145 points)

    Considering what you said earlier in this thread about Mail Rules not working for outgoing emails does this mean that I can't tack this script on to a Mail rule created along the lines of 'Any' email 'From Contains' 'whoever@companydomain.com' then 'Run ApleScript' so as to automate the process?

  • twtwtw Level 5 Level 5 (4,900 points)

    Look, if I were going to do what you're trying to do, I would stop fiddling with rules and set up a launch agent that would periodically (once a day, once an hour, whatever your needs are) forward all unforwarded emails to the people they need to be forwarded to.  That requires two components: a launchd plist (which goes in ~/Library/LaunchAgents or /Library/LaunchAgents) and a script file (which can go anywhere, but /Library/Scripts would be conventional).  the launchd plist looks soemthing like this (save it as user.mail.forward.plist):

     

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

    <plist version="1.0">

    <dict>

              <key>Label</key>

              <string>user.mail.forward</string>

              <key>ProgramArguments</key>

              <array>

                        <string>osascript</string>

                        <string>/Path/to/scriptfile.scpt</string>

              </array>

              <key>StartCalendarInterval</key>

              <dict>

                        <key>Hour</key>

                        <integer>18</integer>

                        <key>Minute</key>

                        <integer>0</integer>

              </dict>

    </dict>

    </plist>

     

    That runs the script once a day at 6:00 pm.  the script itself would look something like this:

     

    property lastForwardingDate : (current date)

    property bccRecipientAddresses : {"email1@somewhere.com", "email2@somewhere.com"}

     

    tell application "Mail"

              set unforwardedMessages to messages of mailbox "Sent" of account "gmail" whose date sent > lastForwardingDate

              repeat with thisMessage in unforwardedMessages

                        set messageToForward to redirect thisMessage with opening window

                        tell messageToForward

                                  repeat with thisRecipient in bccRecipientAddresses

      make new bcc recipient at end of bcc recipients with properties {address:thisRecipient}

                                  end repeat

                                  set lastForwardingDate to date sent of messageToForward

      -- send

                        end tell

              end repeat

    end tell


    This is a testing version, where the email pops up so you can see what it looks like before it gets sent.  in the final version you'll want to delete the 'with opening window' phrase (so that the emails are generated invisibly in the background) and uncomment the 'send' line (so that the emails are sent automatically).

     

    You'll can try replacing 'forward' with 'redirect': redirect sends the email to the new recipient exactly like the old; forward sends a quoted version.  not sure which you prefer.

     

    warn people that they should expect to see extra email activity periodically, and that they may see emails appear in their drafts mailbox (which will happen if they are offline when the script runs - they'll get sent later). Also, you should probably set permissions so people can't edit the script file - if it gets saved again it recompiles, which will reset lastForwardingDate to the date it recompiles, and may cause some emails not to get forwarded.

  • Sean Dale1 Level 5 Level 5 (4,145 points)

    Thank you very much for this. It has certainly given me some food for thought. You have obviously put some real thought in to this which is why I feel a bit of a fool because having given up on finding a way of getting this rule to work I decided to have another go at it and may have cracked it. I'll have to do some more testing to be sure but will post back if it is successful.

  • Camelot Level 8 Level 8 (46,195 points)
    As twtwtw says what I need to do is get a copy of all outgoing emails from any given worker on the road BCC'd to their manager in the office.

     

    IMHO this is something better done server-side.

     

    Any client-side implementation is going to be limited to that client only - so you can craft your Mail.app rule but if breaks if the user opts for Entourage, or another email client, or sends a message from the iPhone/iPad etc.

     

    If you're running your own mail server it's pretty easy to add a server-side rule that copies any mail sent to a specific address and the user doesn't need to think twice about it.

  • Sean Dale1 Level 5 Level 5 (4,145 points)

    I'm being forced to revisit this problem due to the inconsistencies of Mail. The idea of using a Launch Agent and a script appears to be my best option. My problem is that my scripting isn't the best so my question is how do I change the solution given by twtwtw to run hourly rather than once a day at 6.00pm

  • twtwtw Level 5 Level 5 (4,900 points)

    if you want it to run once an hour, use this in the plist file:

     

              <key>StartCalendarInterval</key>

              <dict>

                        <key>Minute</key>

                        <integer>0</integer>

              </dict>

     

    This instructs launchd to run the script anytime the minute is 0 - i.e. at the top of the hour - regardless of the hour, day, or etc.  if you want it to run at half-past rather than the top, use 30.  if you look at the man page for launchd.plist under the StartCalendarInterval key, you can see the options available to you.

  • Sean Dale1 Level 5 Level 5 (4,145 points)

    Thanks for such a quick response. Unfortunately I've run into a problem as soon as I went to create the AppleScript,see below

     

    Screen shot 2012-11-09 at 18.00.27.png

     

    Screen shot 2012-11-09 at 18.00.39.png

  • adayzdone Level 2 Level 2 (150 points)

    Just from looking at your screenshot it appears that you copied this line incorrectly:

     

    set unforwardedMessages to messages of mailbox "Sent" of account "gmail" whose date sent > lastForwardingDate

Previous 1 2 Next