vmail-stack/bin/vmail-aliases-add.sh

72 lines
2.5 KiB
Bash
Raw Normal View History

2021-02-10 16:16:23 -08:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
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-10 16:16:23 -08:00
# get domain_id (and thus check if domain already exists)
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."
exit 1
2021-02-10 16:16:23 -08:00
fi
# get mbox id, which also verfies that email address exists
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."
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
2021-02-10 16:16:23 -08:00
# existing alias does not exist, add new one now
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
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