Skip navigation

AppleScript to auto BCC emails from Apple Mail

7734 Views 24 Replies Latest reply: Nov 4, 2013 12:48 PM by braahn1979 RSS Branched to a new discussion.
1 2 Previous Next
Sean Dale1 Level 5 Level 5 (4,145 points)
Currently Being Moderated
Aug 31, 2011 7:20 PM

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,580 points)
    Currently Being Moderated
    Aug 31, 2011 7:43 PM (in response to Sean Dale1)

    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.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Sep 1, 2011 8:00 AM (in response to Sean Dale1)

    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 (45,670 points)
    Currently Being Moderated
    Sep 1, 2011 10:57 AM (in response to Sean Dale1)

    :: 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,580 points)
    Currently Being Moderated
    Sep 1, 2011 11:06 AM (in response to Camelot)

    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.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Sep 1, 2011 12:16 PM (in response to Sean Dale1)

    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.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Sep 1, 2011 2:27 PM (in response to Sean Dale1)
    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.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Nov 9, 2012 8:09 AM (in response to Sean Dale1)

    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.

  • adayzdone Level 2 Level 2 (150 points)
    Currently Being Moderated
    Nov 9, 2012 11:46 AM (in response to Sean Dale1)

    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

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.