Applescript to auto cc outgoing mail from specific account

Discussion in 'Mac Programming' started by Stevp1, May 15, 2015.

  1. Stevp1 macrumors regular

    Joined:
    Dec 8, 2003
    #1
    Hey all, hope I can get some guidance on an Applescript.

    My CRM allows me to link an email recipient to their contact entry if I cc: or forward the email to a custom email address as well as the recipient.

    I have multiple email accounts and only need to have this feature on one of them.

    I would like to create a script to automatically bcc: or cc: this custom address every time I send an email from this specific account, so it gets filed with the contact entry in the CRM. This would be for the Mail app.


    Any help would be appreciated. Thanks!
     
  2. 0d085d macrumors regular

    Joined:
    Apr 23, 2015
    #2
    Sadly you can't hijack an email as it's being sent, but you can write an Applescript to help you with composition:

    Code:
    set sendFrom to "Joe Bloggs <joe.bloggs@example.com>"
    set crmAddress to "crm@example.com"
    set crmName to "CRM Bot"
    set newMessageDefaultToCRM to true
    
    tell application "Mail"
    	activate
    	set theSelection to selection
    	if theSelection is {} then
    		set m to make new outgoing message
    		if newMessageDefaultToCRM then
    			tell m to set sender to sendFrom
    		end if
    	else
    		repeat with thisMessage in theSelection
    			set m to reply thisMessage
    			exit repeat
    		end repeat
    	end if
    	tell m
    		if sender is equal to sendFrom then
    			set recip to make new cc recipient at end with properties {address:crmAddress, name:crmName}
    		end if
    	end tell
    end tell
    This will reply to a selected message, or create a new message if no message is selected.

    You can put this in ~/Library/Scripts/Applications/Mail and it will appear in the menu bar when Mail is open (you'll need to tick "Show Script menu in menu bar" in the Applescript editor preferences).

    Alternatively, you could create a new service in Automator and paste that Applescript into a "Run Applescript" block. You can then assign a keyboard shortcut to your service in System Preferences -> Keyboard -> Shortcuts -> App Services.
     

Share This Page