Skip to main content
Question

Extension attribute for Mail.app's configured accounts?

  • February 21, 2012
  • 4 replies
  • 32 views

Does anyone have any template for checking what email accounts are configured for Mail.app?

We're trying to audit any 10.6.x users that have set Mail to access Exchange. TIA.

4 replies

Forum|alt.badge.img+6
  • Contributor
  • February 21, 2012

This script might be more specific than you want, and you'll have to muck with the regular expressions. -----------------

#!/bin/sh

# This script outputs a comma separated list of the usernames for all Exchange accounts in Mail.app. # It was written for an environment with predictable e-mail addresses: blahblah@domain.com, that point at blahblah@exchange.domain.com. Take the time to make sure the regular expressions below will work in your environment.

lastUser=/usr/bin/last -1 -t console | awk '{print $1}'

# Check the plist for for Mail.app. The Two grep commands will have to be modified for your environment.
mailAppUser=/usr/libexec/PlistBuddy -c 'Print :MailSections' /Users/"$lastUser"/Library/Preferences/com.apple.mail.plist | grep -o -i ews://[a-zA-Z0-9]@[a-zA-Z0-9].exchange.domain.com/ | grep -o -i [a-zA-Z0-9]@[a-zA-Z0-9].exchange.domain.com | tr ' ' ','

# Now list all configured Exchange accounts in a comma separated list, removing any duplicates. The first sed command will have to be modified for your environment. crazyOutput=echo "$mailAppUser"| sed -E s:@[a-zA-Z0-9.]*domain.com::g | tr ',' ' ' | sort | uniq | tr ' ' ',' | sed -E s:,:: | sed -E s:,$::

echo "<result>$crazyOutput</result>"


Forum|alt.badge.img+12
  • Contributor
  • February 21, 2012

Use defaults read to get the data you want in this case.

Then we use awk. Resulting in a MUCH easier to read command.

$ defaults read ~/Library/Preferences/com.apple.mail MailSections | awk -F/ '/@/{print $3}'
rmanly@server.glenbrook225.org
ryan.manly@imap.gmail.com
  • use / as the field seperator (instead of space).
  • Find lines with an @ symbol
  • print the third field

I added my gmail account just to make sure it would show everything. I don't have a pop account to test with but I imagine it is the same.


Forum|alt.badge.img+12
  • Contributor
  • February 22, 2012

Here is a finished script that will find all the configured mail addresses for all users.

EDIT: this includes the server portion of the address which could be removed if required

#!/bin/bash

# in the nearly impossible case that /Users/* expands to nothing, don't literally use '/Users/*'
shopt -s nullglob

# create an empty array
all_emails=()

# for everything in /Users/ directory
for user in "/Users/*"; do

    # check for the plist
    if [[ -e "${user}/Library/Preferences/com.apple.mail.plist" ]]; then

        # read output of defaults into all_emails array popping in one element at a time and putting the user in first
        while read; do
            all_emails+=("${user}")
            all_emails+=("$REPLY")
        done < <(defaults read "${user}/Library/Preferences/com.apple.mail" MailSections | awk -F/ '/@/{print $3}')

    fi

done

echo "<result>${all_emails[*]}</result>"

Here is the output.

# ./all_configured_email.bash 
<result>/Users/ryan rmanly@foo.glenbrook225.org /Users/ryan ryan.manly@imap.gmail.com /Users/test rmanly@bar.glenbrook225.org</result>

When the EAs can take line endings again it would be a little easier to read if the list appeared like this:

/User/ryan
rmanly@blah.com
/User/test
test@test.com

I didn't bother cuz it won't show up that way right now anyway.

EDIT: added more quotes


Forum|alt.badge.img+12
  • Contributor
  • February 22, 2012

And if you JUST want to see who has setup an address for the Exchange environment you are looking for replace the /@/ search in awk with that domain. I would also remove the all_emails+=($user}) bit at that point too maybe if you think there will be more that do NOT have that setup than do.