Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

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 12:27 AM in response to twtwtw

"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.

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 makenewoutgoing messageat end of outgoing messageswith properties {visible:true}

tell newMess

repeat with thisRecipient in bccRecipients as list


makenewbcc recipientat end of bcc recipientswith properties {address:thisRecipient}

end repeat

end tell

end tell


running it from the script menu would probably be easiest.

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.

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 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.

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.

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.

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 ID.