2021-02-10 16:16:23 -08:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
2022-08-22 13:34:20 -07:00
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2021-02-10 16:16:23 -08:00
2021-04-02 12:02:50 -07:00
# load include file
source $( dirname $0 ) /vmail.sh
2021-02-10 16:16:23 -08:00
help( )
{
thisfilename = $( basename -- " $0 " )
echo " $thisfilename "
echo "Add email alias to vmail system"
echo ""
2021-10-15 15:17:29 -07:00
echo " usage: $thisfilename -e <email> -a <alias> [-h] "
2021-02-10 16:16:23 -08:00
echo ""
echo " -h Print this help."
2021-10-15 15:17:29 -07:00
echo " -e <email> Existing email address that will receive alias emails."
echo " -a <alias> Alias that will direct to existing email address."
2021-02-10 16:16:23 -08:00
echo ""
2021-10-15 15:17:29 -07:00
echo " Aliases are for delivering an email to another address in the same domain."
echo " Use Forwarding for directing emails to an address in another domain."
2021-02-10 16:16:23 -08:00
echo " 'email' should be the full address 'alias' just the local part (username)."
echo " .e.g to have info@example.com delivered (aliased) to joe@example.com do:"
2021-10-15 15:17:29 -07:00
echo " $thisfilename -e joe@example.com -a info "
2021-02-10 16:16:23 -08:00
}
2021-10-15 15:17:29 -07:00
vmail:getoptions " $@ "
# check for email
if [ [ -z $email ] ] ; then
echo "email address is required"
exit
2021-02-10 16:16:23 -08:00
fi
2021-10-15 15:17:29 -07:00
# check for alias
if [ [ -z $alias ] ] ; then
echo "alias is required"
exit
fi
2021-02-12 09:47:26 -08:00
2021-02-10 16:16:23 -08:00
# get domain_id (and thus check if domain already exists)
2021-02-11 14:20:30 -08:00
domain_id = ` mysql --defaults-extra-file= $MYSQL_CONNECTION_INFO_FILE -s -r -N -e " SELECT id from vm_domains WHERE domain=' $domain '; " `
2021-02-10 16:16:23 -08:00
if [ -z $domain_id ] ; then
echo " ERROR: Domain $domain does not exist. "
2021-02-11 14:20:30 -08:00
exit 1
2021-02-10 16:16:23 -08:00
fi
# get mbox id, which also verfies that email address exists
2021-02-11 14:20:30 -08:00
mbox_id = ` mysql --defaults-extra-file= $MYSQL_CONNECTION_INFO_FILE -s -r -N -e " SELECT id FROM vm_mboxes WHERE domain_id=' $domain_id ' AND mbox=' $mbox '; " `
2021-10-15 16:15:05 -07:00
if [ [ -z $mbox_id ] ] ; then
2021-02-10 16:16:23 -08:00
# mbox does not exist, can't create alias
echo " ERROR: Address to Alias To ( $email ) does not exist. "
2021-02-11 14:20:30 -08:00
exit 1
2021-10-15 16:15:05 -07:00
elif [ [ $mbox_id -gt 0 ] ] ; then
2021-02-10 16:16:23 -08:00
# verified mbox, check for existing alias
2021-10-15 15:17:29 -07:00
existing_alias = ` mysql --defaults-extra-file= $MYSQL_CONNECTION_INFO_FILE -s -r -N -e " SELECT vm_aliases.id FROM vm_aliases WHERE vm_aliases.alias=' $alias ' AND vm_aliases.mbox_id=' $mbox_id '; " `
2021-10-15 16:15:05 -07:00
if [ [ -z $existing_alias ] ] ; then
2022-05-20 15:22:40 -07:00
# existing alias does not exist, make sure mbox with alias name does not exist
alias_mbox_id = ` mysql --defaults-extra-file= $MYSQL_CONNECTION_INFO_FILE -s -r -N -e " SELECT id FROM vm_mboxes WHERE domain_id=' $domain_id ' AND mbox=' $alias '; " `
if [ [ -z $alias_mbox_id ] ] ; then
# mailbox does not exist, add alias
dbcmd = " mysql --defaults-extra-file= $MYSQL_CONNECTION_INFO_FILE -e 'INSERT INTO vm_aliases SET vm_aliases.alias=\" $alias \", vm_aliases.mbox_id=\" $mbox_id \";' "
eval $dbcmd
else
echo " ERROR: Email account $alias @ $domain exists, can't create alias with the same name. "
exit 1
fi
2021-02-10 16:16:23 -08:00
else
2021-10-15 15:17:29 -07:00
echo " ERROR: Alias for $alias To $email already exists. "
exit 1
2021-02-10 16:16:23 -08:00
fi
else
# db query error
echo "ERROR: System error querying vmail database."
exit 1
fi