This is a script I wrote to do it all with a single command. - though it assumes you have already added the new domain to to Sendmail's local host names.
To do this first step:
cd /etc/mail/
pico local-host-names
Add a line to /etc/mail/local-host-names with the value of your domain name.
This tells sendmail what domains it responds to as local. In other words, mail to these domains will be delivered to local users if they exist or bounced if they don't exist.
You put this script in the bin directory of your home directory (from memory), then create a symbolic link to it so that you can run it from anywhere ... can't remember where the hell I put the symbolic link ... (it was my first script)
# addmail.sh - Adds new email address
#####################################
# Takes 3 compulsory options: email address, password and system username
# Define the usage statement as a variable
usage="USAGE: addmail -e email -p password -u username"
# The correct number of positional parameters is 6 (3 options & 3 arguments)
# Check the number of positional parameters and exit if incorrect
nopt=$#
if [ ${nopt} -ne 6 ]
then
echo "ERROR: Incorrect number of positional parameters"
echo "${usage}"
exit 1
fi
# As getopts peels the command line options off, it stores them in variable opt
# Use opt as the test value in the case statement
# For each option, assign the arguments passed ($OPTARG) to a variable
# Define a default case that prints usage and exits if invalid options are used
while getopts ":e
:u:" opt;
do
case $opt in
e ) eopt=$OPTARG ;;
p ) popt=$OPTARG ;;
u ) uopt=$OPTARG ;;
\? ) echo ${usage}
exit 1
esac
done
# Check whether the Virtusertable file has been backed up already today.
# If it has not been backed up, then back it up using today's date
today=$(date --iso-8601)
if [ ! -e /etc/mail/virtusertable.${today} ]; then
cp /etc/mail/virtusertable /etc/mail/virtusertable.${today}
fi
# Create the user and re-enter the password (-p sometimes fails)
# Change the permissions on the mailbox and add the user to the virtusertable
# Then make the mail file, restart sendmail and print out the user details
# Use the options passed to the addmail script
# Only execute each command if the proceeding command completed successfully
adduser -g mail -d /home/${uopt} ${uopt} &&
echo "${popt}" | passwd --stdin ${uopt} &&
chown -R ${uopt}.mail /home/${uopt} &&
echo "${eopt}"$'\t'"${uopt}" >> /etc/mail/virtusertable &&
make -C /etc/mail &&
/etc/rc.d/init.d/sendmail restart
echo "Email: ${eopt:-"Did not use option -e"}"
echo "Password: ${popt:-"Did not use option -p"}"
echo "Username: ${uopt:-"Did not use option -u"}"