Redesign of script 'sent_userinfo_postfix.sh'.

This commit is contained in:
2020-08-06 01:20:43 +02:00
parent e9a69d2792
commit 12870d71e5
6 changed files with 273 additions and 41 deletions

View File

@ -1,11 +1,15 @@
#!/usr/bin/env bash
script_dir="$(dirname $(realpath $0))"
conf_dir="${script_dir}/conf"
conf_file="${conf_dir}/sent_userinfo_postfix.conf"
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
file_dir="${working_dir}/files"
conf_dir="${working_dir}/conf"
log_dir="${working_dir}/log"
tmp_dir="$(mktemp -d)"
logfile="${script_dir}/log/sent_userinfo_postfix.$(date +%Y-%m-%d-%H%M).log"
conf_file="${conf_dir}/sent_userinfo_postfix.conf"
logfile="${working_dir}/log/sent_userinfo_postfix.$(date +%Y-%m-%d-%H%M).log"
LOCK_DIR="/tmp/$(basename $0).$$.LOCK"
#---------------------------------------
#-----------------------------
@ -13,12 +17,16 @@ logfile="${script_dir}/log/sent_userinfo_postfix.$(date +%Y-%m-%d-%H%M).log"
#-----------------------------
#---------------------------------------
DEFAULT_user_info_file="${conf_dir}/sent_userinfo_postfix.email"
DEFAULT_message_body_file="${file_dir}/sent_userinfo_postfix.message"
DEFAULT_db_type="pgsql"
DEFAULT_db_name="postfix"
DEFAULT_mail_user="vmail"
DEFAULT_mail_group="vmail"
if [[ -f "${file_dir}/sent_userinfo_postfix.subject" ]]; then
DEFAULT_email_subject="$(cat "${file_dir}/sent_userinfo_postfix.subject" | head -1)"
fi
#---------------------------------------
#-----------------------------
@ -29,9 +37,10 @@ DEFAULT_mail_group="vmail"
clean_up() {
# Perform program exit housekeeping
rm -rf $tmp_dir
rm -rf $LOCK_DIR
exit $1
}
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
@ -59,6 +68,9 @@ fatal(){
echo_ok() {
echo -e "\033[75G[ \033[32mok\033[m ]"
}
echo_done() {
echo -e "\033[75G[ \033[32mdone\033[m ]"
}
echo_failed() {
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
}
@ -66,12 +78,36 @@ echo_skipped() {
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
}
# ----------
# - Jobhandling
# ----------
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
# -
trap clean_up SIGHUP SIGINT SIGTERM
# - Create lock directory '$LOCK_DIR"
#
mkdir "$LOCK_DIR"
# ----------
# - Main part of script
# ----------
clear
echo ""
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
echo ""
# ----------
# - Read Configuration file
# ----------
echo ""
echo ""
@ -87,15 +123,19 @@ else
fi
fi
if [[ -z "$email_from" ]] ; then
fatal "Missing Mail Sender Address (parameter 'email_from')."
if [[ -n "$email_from" ]] ; then
DEFAULT_email_from="$email_from"
fi
if [[ -z "$email_from_org" ]] ; then
fatal "Missing Mail Sender Organisation (parameter 'email_from_org')."
if [[ -n "$email_from_org" ]] ; then
DEFAULT_email_from_org="$email_from_org"
fi
[[ -n "$user_info_file" ]] || user_info_file="$DEFAULT_user_info_file"
if [[ -n "$message_body_file" ]] ; then
$DEFAULT_message_body_file="$message_body_file"
fi
[[ -n "$db_type" ]] || db_type="$DEFAULT_db_type"
if [[ "$db_type" != "pgsql" ]] && [[ "$db_type" != "mysql" ]]; then
@ -119,10 +159,6 @@ fi
[[ -n "$mail_user" ]] || mail_user="$DEFAULT_mail_user"
[[ -n "$mail_group" ]] || mail_group="$DEFAULT_mail_group"
if [[ ! -f $user_info_file ]];then
fatal "User Info-file to send '$user_info_file' does not exist !!"
fi
echo ""
echo -e "\033[32m--\033[m"
@ -134,7 +170,7 @@ _TEST_RUN=
while [ "$_TEST_RUN" != "yes" -o "$_TEST_RUN" != "no" ] ; do
echononl "\033[1mTest run? [yes/no]:\033[m "
read _TEST_RUN
## - To lower case
# - To lower case
_TEST_RUN=${_TEST_RUN,,}
if [ "X$_TEST_RUN" = "X" ]; then
echo -e "\n\t\033[33m\033[1mAn entry is required! Type 'yes' or 'no'\033[m.\n"
@ -171,12 +207,131 @@ if $TEST_RUN ; then
done
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Give the Sender E-Mail for the user info."
echo ""
echo ""
email_from=""
if [[ -n "$DEFAULT_email_from" ]]; then
echononl "Sender E-Mail [$DEFAULT_email_from]: "
read email_from
# - To lower case
email_from=${email_from,,}
if [[ "X$email_from" = "X" ]] ; then
email_from="$DEFAULT_email_from"
fi
else
while [ "X$email_from" = "X" ]; do
echononl "Sender E-Mail: "
read email_from
# - To lower case
email_from=${email_from,,}
if [ "X$email_from" = "X" ]; then
echo ""
echo -e "\n\t\033[33m\033[1m'Sender E-Mail' is required!\033[m\n"
continue
fi
done
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Give the Sender Organisation or the Sender Name."
echo ""
echo ""
email_from_org=""
if [[ -n "$DEFAULT_email_from_org" ]]; then
echononl "Sender Organisation/Name [$DEFAULT_email_from_org]: "
read email_from_org
if [[ "X$email_from_org" = "X" ]] ; then
email_from_org="$DEFAULT_email_from_org"
fi
else
while [ "X$email_from_org" = "X" ]; do
echononl "Sender Organisation/Name: "
read email_from_org
if [ "X$email_from_org" = "X" ]; then
echo ""
echo -e "\n\t\033[33m\033[1m'Sender Organisation/Name' is required!\033[m\n"
continue
fi
done
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Give the Subject of the user info e-mail."
echo ""
echo ""
email_subject=""
if [[ -n "$DEFAULT_email_subject" ]]; then
echononl "Subject [$DEFAULT_email_subject]: "
read email_subject
if [[ "X$email_subject" = "X" ]] ; then
email_subject="$DEFAULT_email_subject"
fi
else
while [ "X$email_subject" = "X" ]; do
echononl "Subject: "
read email_subject
if [ "X$email_subject" = "X" ]; then
echo ""
echo -e "\n\t\033[33m\033[1m'Subject' is required!\033[m\n"
continue
fi
done
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Give the path to the file containing message (body) of the info e-mail."
echo ""
echo ""
message_body_file=""
if [[ -n "$DEFAULT_message_body_file" ]]; then
echononl "Path to message file [$DEFAULT_message_body_file]: "
read message_body_file
if [[ "X$message_body_file" = "X" ]] ; then
message_body_file="$DEFAULT_message_body_file"
fi
else
while [ "X$message_body_file" = "X" ]; do
echononl "Path to message file: "
read message_body_file
if [ "X$message_body_file" = "X" ]; then
echo ""
echo -e "\n\t\033[33m\033[1m'Path to message file' is required!\033[m\n"
continue
fi
done
fi
if [[ -z "$email_from" ]] ; then
fatal "Missing Mail Sender Address (parameter 'email_from')."
fi
if [[ -z "$email_from_org" ]] ; then
fatal "Missing Mail Sender Organisation (parameter 'email_from_org')."
fi
if [[ -z "$email_subject" ]] ; then
fatal "Missing Subject (parameter 'email_subject')."
fi
if [[ ! -f $message_body_file ]];then
fatal "User Info-file to send '$message_body_file' does not exist !!"
fi
echo ""
echo ""
echo -e "\033[32mSettings for script \033[37m\033[1msent_userinfo_postfix.sh\033[m"
echo ""
echo " File containing the mail-body.........: $user_info_file"
echo " File containing the mail-body.........: $message_body_file"
echo ""
if [[ "$db_type" = "pgsql" ]] ; then
echo " Type of postfix databae...............: PostgreSQL ($db_type)"
@ -189,6 +344,7 @@ fi
echo ""
echo " Mail Sender Address...................: $email_from"
echo " Mail Sender Organisation..............: $email_from_org"
echo " Subject of the userinfo e-mail........: $email_subject"
echo ""
echo " Mail User.............................: $mail_user"
echo " Mail Group............................: $mail_group"
@ -239,22 +395,38 @@ fi
curdir=`pwd`
rc_done="\033[71G[ \033[32mdone\033[m ]"
rc_failed="\033[71G[ \033[31m\033[1mfailed\033[m ]"
if [ ! -f $user_info_file ]; then
if [ ! -f $message_body_file ]; then
fatal "File containing User Info not found!"
fi
pwd=`pwd`
cd /tmp
echo ""
echo -e "\n\t --- Prepare Info E-Mail --\n" | tee -a $logfile
cp "$message_body_file" "${LOCK_DIR}/message_tmp.txt"
echo "$email_subject" > ${LOCK_DIR}/subject_tmp.txt
# - Convert to UTF-8
# -
iconv -t UTF8 -o "${LOCK_DIR}/message_tmp_UTF8.txt" "${LOCK_DIR}/message_tmp.txt"
iconv -t UTF8 -o "${LOCK_DIR}/subject_tmp_UTF8.txt" "${LOCK_DIR}/subject_tmp.txt"
# - encode base64
# --
ENCODED_SUBJECT="=?utf-8?B?$(cat ${LOCK_DIR}/subject_tmp_UTF8.txt | base64 --wrap=0)?="
ENCODED_MESSAGE="$(cat ${LOCK_DIR}/message_tmp_UTF8.txt | base64)"
content_type='Content-Type: text/plain;\n charset="utf-8"'
transfer_encoding='Content-Transfer-Encoding: base64'
content_disposition='Content-Disposition: inline'
echo ""
echo -e "\n\t --- Sending userinfo into all local virtual mailboxes --\n" | tee -a $logfile
## - list of local virtual domains
## -
if $TEST_RUN ; then
@ -299,18 +471,24 @@ for domain in $domains ;do
fi
for local_part in $local_parts ; do
cp "$user_info_file" "$tmp_dir"
perl -i -n -p -e "s/%email_to%/$local_part\@$domain/" "${tmp_dir}/$(basename $user_info_file)"
perl -i -n -p -e "s/%email_from%/${email_from_regex}/" "${tmp_dir}/$(basename $user_info_file)"
perl -i -n -p -e "s/%email_from_org%/${email_from_org}/" "${tmp_dir}/$(basename $user_info_file)"
echononl "\tSend userinfo to $local_part@$domain.."
cat ${tmp_dir}/$(basename $user_info_file) | /usr/sbin/sendmail -F "$email_from_org" -f $email_from -t "$local_part@$domain"
echo -e "From: ${email_from_org} <${email_from}>
To: $local_part@$domain
Subject: ${ENCODED_SUBJECT}
${content_type}
${transfer_encoding}
${content_disposition}
$ENCODED_MESSAGE
" | /usr/sbin/sendmail -F "$email_from_org" -f $email_from -t
if [ "$?" = "0" ]; then
num_mbox=num_mbox+1
echo -e "$rc_done"
echo_done
echo " [ OK ]: Send userinfo to $local_part@$domain" >> $logfile
else
echo -e "$rc_failed"
echo_failed
echo " [ ERROR ]: Cannot sent userinfo to \"$local_part\@$domain\"!" >> $logfile
num_mbox_failed=num_mbox_failed+1
fi
@ -319,7 +497,7 @@ for domain in $domains ;do
done
if ! $TEST_RUN ; then
mv "$user_info_file" "${user_info_file}.SENT.$(date +%Y-%m-%d)"
mv "$message_body_file" "${message_body_file}.SENT.$(date +%Y-%m-%d)"
fi
echo -e "\n\n----- Statistics -----\n\n\tSent mail to $num_mbox mailboxe(s) of $num_dom domain(s)" | tee -a $logfile