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

87 lines
2.7 KiB
Bash
Raw Normal View History

2022-05-23 15:49:49 -07:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load include file
source $(dirname $0)/vmail.sh
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Add autoresponder"
echo ""
echo "usage: $thisfilename -e <email> -u <subject> -b <body> [-o <mode>] [-s <0|1>] [-h]"
echo ""
echo " -h Print this help."
echo " -e <email> Email address of the autoresponder."
echo " -u <subject> Subject of autresponder emails."
echo " -b <body> Body of autresponder emails."
echo " -o <mode> Mode - either Autoresponder or Vacation. Default is Vacation."
echo " -s <0|1> Status. 0 = disabled/off, 1 = enabled/on. Default is 1."
echo ""
echo " The body must be single quoted and escaped. All single quotes in the body must"
echo " have double back slash like this \\' and all newlines should be encoded as \n."
echo " In Vacaion mode only the first email from a specific address is replied to,"
echo " in Autoresponder mode every incoming email, including repeats, get a response."
exit
}
vmail:getoptions "$@"
# check for email
if [[ -z $email ]]; then
echo "email is required"
exit 1
fi
# check for subject
if [[ -z $subject ]]; then
echo "subject is required"
exit 1
fi
# check for body
if [[ -z $body ]]; then
echo "body is required"
exit 1
fi
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e"
# get mbox id, which also verfies that email address exists
dbquery="SELECT vm_mboxes.id FROM vm_mboxes, vm_domains WHERE vm_mboxes.mbox='$mbox' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain';"
mbox_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $mbox_id ]; then
# mailbox does not exist
echo "ERROR: Email account $email does not exist."
exit 1
elif [ "$mbox_id" -gt '0' ]; then
# verified mbox, check for existing forward
dbquery="SELECT id FROM vm_autoresponders WHERE mbox_id='$mbox_id';"
vm_autoresponders_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $vm_autoresponders_id ]; then
# no autoresponder, add new one now
dbquery="INSERT INTO vm_autoresponders SET mbox_id='$mbox_id', subject='$subject', body='$body'"
if [ ! -z $mode ]; then
dbquery="$dbquery, mode='$mode'"
fi
if [ ! -z $status ]; then
dbquery="$dbquery, status='$status'"
fi
eval $dbcmd $dbcmdopts \"$dbquery\;\"
else
echo "ERROR: Autoresponder for $email already exists, edit or delete and re-create instead."
exit 1
fi
else
# db query error
echo "ERROR: System error querying database."
exit 1
fi