AppleScript to auto BCC emails from Apple Mail

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'

Posted on Aug 31, 2011 7:20 PM

Reply
27 replies

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.

Nov 13, 2012 9:57 AM in response to twtwtw

Ok. After a Restart all of a sudden I get something different if I open the script and try to run it, see below.


User uploaded file


I've looked back at the script to make sure the format wasn't still messed up and it looks exactly like what you suggested I use.


Also,although I created the launchd plist and put it in /LaunchAgents it doesn't appear to run. Do I need to do something else to enable it?

Feb 13, 2013 11:15 AM in response to twtwtw

I've been trying this for a while and once I got past the formatting issues it all seemed to work but I never have managed to get the script to automatically send as it should do.


What seems to happen is that I end up with a lot of hidden emails waiting to go that remain invisible until I quit and re-open Mail at which point I get lots of emails appear in Drafts and a whole lot of draft emails popping up all over the screen.


The launchd.plist is obviously doing what it should so I would guess that I've still got a formatting problem with the script.


User uploaded file

Maybe you could point me in the right direction as I don't seem to be able to see the wood for the trees.

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 redirectthisMessage with opening window

tell messageToForward

repeat with thisRecipient in bccRecipientAddresses


makenewbcc recipientat end of bcc recipientswith 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.

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.

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.

Sep 1, 2011 11:20 AM in response to Camelot

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

Sep 1, 2011 2:07 PM in response to twtwtw

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.

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.

Feb 13, 2013 4:09 PM in response to Sean Dale1

ok, my snippy moment has passed. 😉


My guess is that lastForwardingDate is the thing causing the issue. For example, the first time you run the script after a compile, you set lastForwardingDate to the current date, and then ask for messages sent after lastForwardingDate. Unless you're getting emails from the future that will never return anything. what I suggest you do is set the property to some older date for the first run, and then add in debuggers to see what lastForwardingDate is actually being set to as the script progresses.


Also, you might check to see if a real sender and subject line is included in your outgoing message, or if it's all bcc: or has no subject. If your preferences in Mail are set to give an alert when the subject is blank that could cause the block, and it may not like sending automated bcc: lists (which is spam-like behavior).

Jun 6, 2014 4:05 AM in response to Sean Dale1

I have for the first time in years of using Mac Mail had what appears to be a common experience of a Sent email not having a copy in the mail box. This was an absolutely crucial email and I know it went because I received an Auto out of office reply from one of the recipients.


However in looking now to the tedious failsafe of having to bcc all sent emails to another address - my thinking was to create an address outside Mac Mail ( so that I am not disturbed by Incoming Email Notification for all my Mac Mail aliases).


Upon looking at Mail preferences I see there is STILL no option to Bcc automatically to any email address but Myself. ( There was a complaint about this in the thread still in 2011 and nothing appears to have been done about it despite my reading that there are many examples of Sent emails not being retained).


Does anyone have a solution which does not involve complex Apple Script stuff and other examples already discussed in this thread ( with due respect to the contributors whose technical expertise is awesome but too specialised for many)


best


MS

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript to auto BCC emails from Apple Mail

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.