Skip to content

Instantly share code, notes, and snippets.

@grahampugh
Last active November 25, 2017 10:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahampugh/9cd744289e74868631df829c375c1c20 to your computer and use it in GitHub Desktop.
Save grahampugh/9cd744289e74868631df829c375c1c20 to your computer and use it in GitHub Desktop.
E-Mail IMAP configuration profile and outset script. Takes the long user name of the logged in user to populate the email field. User must supply only the password.
<?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>PayloadIdentifier</key>
<string>
com.apple.mdm.server1.local.%first_uuid%.alacarte</string>
<key>PayloadRemovalDisallowed</key>
<false />
<key>PayloadScope</key>
<string>User</string>
<key>PayloadType</key>
<string>Configuration</string>
<key>PayloadUUID</key>
<string>%first_uuid%</string>
<key>PayloadOrganization</key>
<string>server1.local</string>
<key>PayloadVersion</key>
<integer>1</integer>
<key>PayloadDisplayName</key>
<string>%payload_name%</string>
<key>PayloadContent</key>
<array>
<dict>
<key>PayloadType</key>
<string>com.apple.mail.managed</string>
<key>PayloadVersion</key>
<integer>1</integer>
<key>PayloadIdentifier</key>
<string>
com.apple.mdm.server1.local.%first_uuid%.alacarte.email.%second_uuid%</string>
<key>PayloadUUID</key>
<string>%second_uuid%</string>
<key>PayloadEnabled</key>
<true />
<key>PayloadDisplayName</key>
<string>%payload_name%</string>
<key>EmailAccountDescription</key>
<string>%account_description%</string>
<key>disableMailRecentsSyncing</key>
<true />
<key>allowMailDrop</key>
<false />
<key>PreventMove</key>
<true />
<key>PreventAppSheet</key>
<false />
<key>SMIMEEnabled</key>
<false />
<key>SMIMEEnablePerMessageSwitch</key>
<false />
<key>IncomingMailServerAuthentication</key>
<string>EmailAuthPassword</string>
<key>IncomingMailServerUseSSL</key>
<false />
<key>OutgoingMailServerAuthentication</key>
<string>EmailAuthPassword</string>
<key>OutgoingMailServerUseSSL</key>
<false />
<key>EmailAccountType</key>
<string>EmailTypeIMAP</string>
<key>IncomingMailServerIMAPPathPrefix</key>
<string>%imap_prefix%</string>
<key>EmailAccountName</key>
<string>%full_name%</string>
<key>EmailAddress</key>
<string>%email%</string>
<key>IncomingMailServerHostName</key>
<string>%imap_server%</string>
<key>IncomingMailServerUsername</key>
<string>%email%</string>
<key>OutgoingMailServerHostName</key>
<string>%smtp_server%</string>
<key>OutgoingMailServerUsername</key>
<string>%email%</string>
<key>OutgoingPasswordSameAsIncomingPassword</key>
<true />
<key>updated_at_xid</key>
<integer>4470</integer>
</dict>
</array>
</dict>
</plist>
#!/bin/sh
# path where the profile is situated. Make sure the file is there!
profile_path="/Library/Profiles/mail_profile.mobileconfig"
# server settings etc. Edit to suit
PayloadDisplayName="MyOrg Mail"
EmailAccountDescription="MyOrg Mail"
IMAPPrefix="INBOX"
IMAPServer="mail.myorg.com"
SMTPServer="smtp.myorg.com"
# Payload IDs - these need to be unique if you're adding multiple accounts or profiles.
# You can generate UUIDs at https://www.uuidgenerator.net/version1
FirstUUID="70b3825e-6ad5-11e6-8b77-86f30ca893d3"
SecondUUID="d4f3256c-6ad5-11e6-8b77-86f30ca893d3"
# various variables based on the logged-in Long Name.
# In this example, we want to end up with
# firstname.lastname
longname="$(dscacheutil -q user -a name $(whoami) | fgrep gecos | sed -e 's/.*gecos: \(.*\)/\1/')"
firstname="$(echo $longname | sed -e 's/ .*//')"
firstnamelower="$(echo $firstname | tr '[:upper:]' '[:lower:]')"
lastname="$(echo $longname | sed -e 's/.* //')"
lastnamelower="$(echo $lastname | tr '[:upper:]' '[:lower:]')"
email="$firstnamelower.$lastnamelower@myorg.com"
# tmp path is used so that sed can edit the file inline as the current user
tmp_path="/tmp/mail-$firstnamelower-$lastnamelower.mobileconfig"
cp $profile_path $tmp_path
# rewrite the mobileconfig with the personalised settings
sed -i.bak "s/%first_uuid%/$FirstUUID/g" $tmp_path
sed -i.bak "s/%second_uuid%/$SecondUUID/g" $tmp_path
sed -i.bak "s/%payload_name%/$PayloadDisplayName/g" $tmp_path
sed -i.bak "s/%account_description%/$EmailAccountDescription/g" $tmp_path
sed -i.bak "s/%imap_prefix%/$IMAPPrefix/g" $tmp_path
sed -i.bak "s/%imap_server%/$IMAPServer/g" $tmp_path
sed -i.bak "s/%smtp_server%/$SMTPServer/g" $tmp_path
sed -i.bak "s/%full_name%/$firstname $lastname/g" $tmp_path
sed -i.bak "s/%email%/$email/g" $tmp_path
# install the profile. In this example, the "admin" account does not get mail configured.
if [[ $USER != "admin" ]]; then
/usr/bin/profiles -IvF $tmp_path;
fi
# delete the temporary profile and its backup
rm $tmp_path $tmp_path.bak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment