How to Add an Email Alias using script

While Mac OS X does include a standard Unix mail transport system built around the venerable and remarkably complex sendmail system, it doesn't enable this system by default. To do that requires a number of complex steps, a task more complex than we can discuss in this book. If you do have sendmail running on your Mac OS X box, however, you'll doubtless want to be able to add mail aliases in a simple manner. But mail aliases aren't in /etc/aliases anymore; they're now part of the NetInfo system. This script offers an easy work-around.

Setting up sendmail A variety of sites offer instructions on how to set up sendmail on your Mac OS X system, and some simple freeware applications are even available to set it up automatically. If you want to do it yourself, go to O'Reilly's MacDevCenter at http://www.macdevcenter.com/ and search for "sendmail," or take the easy way out and go to Version Tracker at http://www.versiontracker.com/ and, again, search for "sendmail" to find a variety of freeware configuration utilities. Make sure the solution you try is for your exact version of Mac OS X.


The Code
#!/bin/sh

# addmacalias - Adds a new alias to the email alias database on Mac OS X.
# This presumes that you've enabled sendmail, which can be kind of
# tricky. Go to http://www.macdevcenter.com/ and search for "sendmail"
# for some good reference works.

showaliases="nidump aliases ."

if [ "$(/usr/bin/whoami)" != "root" ] ; then
echo "$(basename $0): You must be root to run this command." >&2
exit 1
fi

if [ $# -eq 0 ] ; then
echo -n "Alias to create: "
read alias
else
alias=$1
fi

# Now let's check to see if that alias already exists...
if $showaliases | grep "${alias}:" >/dev/null 2>&1 ; then
echo "$0: mail alias $alias already exists" >&2
exit 1
fi

# Looks good. let's get the RHS and inject it into NetInfo

echo -n "pointing to: "
read rhs # the right-hand side of the alias

niutil -create . /aliases/$alias
niutil -createprop . /aliases/$alias name $alias
niutil -createprop . /aliases/$alias members "$rhs"

echo "Alias $alias created without incident."

exit 0

How It Works
If you've studied Script #94, Adding a User to a Mac OS X System, you should immediately see all the similarities between that script and this one, including the test for root user and the invocations to niutil with the flags -create and -createprop.

The most interesting snippet in this script is the test to see if the alias already exists:

if $showaliases | grep "${alias}:" >/dev/null 2>&1 ; then
echo "$0: mail alias $alias already exists" >&2
exit 1
fi

It's a good example of how to properly use the result of a command as a test while discarding any output, either to stdout or stderr. The notation >/dev/null discards stdout, of course, and then the odd notation 2>&1 causes output device #2, stderr, to be mapped to output device #1, stdout, also effectively routing stderr to /dev/null.

Running the Script
This script is fairly flexible: You can specify the alias you'd like to create on the command line, or it'll prompt for the alias if you've forgotten. Otherwise, it prompts for needed fields and has no command flags.

The Results
$ sudo addmacalias
Alias to create: gareth
pointing to: gareth@hotmail.com
Alias gareth created without incident.

Hacking the Script
It would be quite easy to add an -l flag or something similar to addmacalias to produce a listing of all current mail aliases, and that would significantly improve the utility of this simple script.