vmail-stack/bin/vmail-autoresponders-add.sh
2023-11-27 14:48:55 -08:00

91 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# 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)
# 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 base64 encoded, this is to handle special characters and html entities."
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 verfies that email address exists
dbquery="SELECT id FROM vm_mboxes WHERE mbox='$mbox' AND 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 autoresponder
dbquery="SELECT id FROM vm_autoresponders WHERE mbox='$mbox' AND domain='$domain';"
autoresponder_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [[ -z $autoresponder_id ]]; then
# no autoresponder, add new one now
dbquery="INSERT INTO vm_autoresponders SET mbox='$mbox', domain='$domain', subject='$subject', body=FROM_BASE64('$body')"
if [ ! -z $mode ]; then
dbquery="$dbquery, mode='$mode'"
fi
if [ ! -z $status ]; then
dbquery="$dbquery, status='$status'"
fi
eval $dbcmd $dbcmdopts \"$dbquery\;\"
elif [ "$autoresponder_id" -gt '0' ]; then
echo "ERROR: Autoresponder for $email already exists, edit or delete and re-create instead."
exit 1
else
# db query error
echo "ERROR: System error querying database."
exit 1
fi
else
# db query error
echo "ERROR: System error querying database."
exit 1
fi