jeff-j
5/20/2015 - 1:36 AM

Automate/Push fix to disable Yosemite Mail feature: "Automatically detect and maintain account settings"

Automate/Push fix to disable Yosemite Mail feature: "Automatically detect and maintain account settings"

READ ME: fixMailAutoDetect

Yosemite Mail.app added a feature to automatically detect and maintain account settings for SMTP and IMAP. With locked down servers, you may prefer to dictate and not hope for the correct settings.

This script will disable the feature on the target domain

usage

./fixMail TargetDomain [LocalUser]
./fixMail topicdesk.com jeff

topicdesk.com is our search string for IMAP/SMTP accounts to edit. jeff is my local computer account: /Users/jeff LocalUser is optional and defaults to the currently logged in user.

###Updating many machines Push this script via ARD, SSH, Meraki MDM, etc. One example, put the script in a web-accessible location, curl and pipe to bash.

curl http://myserver.com/scripts/fixMail | bash -s -- domain.com
curl http://myserver.com/scripts/fixMail | bash -s -- domain.com richard

#!/bin/bash

# fixMail
# Disables Yosewmite Mail SMTP/IMAP 'Automatically detect and maintain acount settings' option
# labs@topicdesk.com

# usage
# ./fixMail TargetDomain [LocalUser]
# ./fixMail topicdesk.com jeff
# topicdesk.com is our search string for IMAP/SMTP accounts to edit.
# jeff is my local computer account: /Users/jeff
# LocalUser is optional and defaults to the currently logged in user.

echo -e "\n\n"

# Check PlistBuddy
[ ! -f /usr/libexec/PlistBuddy ] && ( echo -e "Missing: /usr/libexec/PlistBuddy\nCannot continue\n"; exit 1 )

[ -n "$1" ] && domain="$1" || (echo "You must specify a domain: fixMail TargetDomain [LocalUser]"; exit 1)
[ -n "$2" ] && username="$2" || username=$(who | grep console | awk '{ print $1 }' | head)

# get user home directory
userhome=$(dscl /Search -read /Users/$username NFSHomeDirectory) || (echo "Could not find home directory for: $username"; exit 1)

echo "Target Domain: $domain"
plist="$(echo $userhome | awk '{ print $2 }')/Library/Mail/V2/MailData/Accounts.plist"
[ -f "$plist" ] &&  echo "Found plist: $plist" || ( echo -e "Missing: $plist\nCan not continue\n" exit 1 )

# backup plist 
plistBak="/tmp/Accounts.plist.`date  +"%Y-%m-%d_%Hh%Mm"`"
cp "$plist" "$plistBak"
echo "plist backed up to to: $plistBak"
 
# crudely quit Mail
killall Mail 2> /dev/null

set_smtp() { 
  /usr/libexec/PlistBuddy -c "Set :DeliveryAccounts:$COUNTER:$1 $2" "$plist"
  echo "SMTP Updated"
  }

set_imap() {
  /usr/libexec/PlistBuddy -c "Set :MailAccounts:$COUNTER:$1 $2" "$plist"
  echo "IMAP Updated"
  }

# Read each SMTP Account and edit only entries which match the target domain.
COUNTER=0
grep "<string>SMTPAccount</string>" "$plist" 2>/dev/null | while read line
do
  if /usr/libexec/PlistBuddy -c "Print :DeliveryAccounts:$COUNTER:Hostname" "$plist" 2>/dev/null | grep -q "$domain"; then
    set_smtp ConfigureDynamically 0
    # other setting can be included here. See ReadMe
  fi   
  COUNTER=$[$COUNTER +1]
done

# Read MailAccounts accounts and edit those matching $domain
COUNTER=0
grep "AccountPath" "$plist" 2>/dev/null | while read line
do
  if /usr/libexec/PlistBuddy -c "Print :MailAccounts:$COUNTER:AccountPath" "$plist" 2>/dev/null | grep -q "$domain"; then
    set_imap ConfigureDynamically 0
    # other setting can be included here. See ReadMe
  fi
  COUNTER=$[$COUNTER +1]
done
 
# clear defaults cache
killall cfprefsd 2>/dev/null
defaults read "$plist" > /dev/null

echo Done

exit 0