#!/usr/bin/env bash script_dir="$(dirname $(realpath $0))" conf_dir="${script_dir}/conf" conf_file="${conf_dir}/sent_userinfo_postfix.conf" tmp_dir="$(mktemp -d)" logfile="${script_dir}/log/sent_userinfo_postfix.$(date +%Y-%m-%d-%H%M).log" #--------------------------------------- #----------------------------- # Setting Defaults #----------------------------- #--------------------------------------- DEFAULT_user_info_file="${conf_dir}/sent_userinfo_postfix.email" DEFAULT_mail_user="vmail" DEFAULT_mail_group="vmail" #--------------------------------------- #----------------------------- # Base Function(s) #----------------------------- #--------------------------------------- clean_up() { # Perform program exit housekeeping rm -rf $tmp_dir exit $1 } echononl(){ echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo "$*\\c" 1>&2 else echo -e -n "$*" 1>&2 fi rm /tmp/shprompt$$ } warn (){ echo "" echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" echo "" } fatal(){ echo "" echo -e "[ \033[31m\033[1mFehler\033[m ]: $*" echo -e "\n\t Script was interupted!\n" echo clean_up 1 } echo_ok() { echo -e "\033[75G[ \033[32mok\033[m ]" } echo_failed() { echo -e "\033[75G[ \033[1;31mfailed\033[m ]" } echo_skipped() { echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" } trap clean_up SIGHUP SIGINT SIGTERM clear echo "" echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .." echo "" echo "" echo "" echononl " Loading default Configuration values from $(basename ${conf_file}).." if [[ ! -f "$conf_file" ]]; then echo_skipped else source "${conf_file}" > /dev/null 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed fi 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 [[ -n "$user_info_file" ]] || user_info_file="$DEFAULT_user_info_file" [[ -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" echo "" echo "Is this a tset-run, sending the user info to only one given address?" echo "" echo "" _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 _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" _TEST_RUN="" continue fi if [ "$_TEST_RUN" = "yes" -o "$_TEST_RUN" = "no" ] ; then break fi echo -e "\n\t\033[33m\033[1mWrong entry! Type 'yes' or 'no'.\033[m\n" done if [[ "$_TEST_RUN" = "yes" ]]; then TEST_RUN=true else TEST_RUN=false fi if $TEST_RUN ; then echo "" echo -e "\033[32m--\033[m" echo "" echo "Give the recipient address for the test run." echo "" echo "" test_email="" while [[ "X$test_email" = "X" ]] ; do echononl "\033[1mRecipient address for test run:\033[m " read test_email if [[ "X$test_email" = "X" ]] ; then echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m" fi done 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 "" echo " Mail Sender Address...............: $email_from" echo " Mail Sender Organisation..........: $email_from_org" echo "" echo " Mail User.........................: $mail_user" echo " Mail Group........................: $mail_group" echo "" echo " Test Run..........................: $TEST_RUN" if $TEST_RUN ; then echo " Recipient address for test run....: $test_email" fi echo "" if ! $TEST_RUN ; then warn "This is \033[1mNOT\033[m a test run. All local mailboxes will receive the user info e-mail." fi echo "" OK= while [ "$OK" != "yes" -o "$OK" != "no" ] ; do echononl "\033[1mParameters ok? [yes/no]:\033[m " read OK ## - To lower case OK=${OK,,} if [ "X$OK" = "X" ]; then echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" OK="" continue fi if [ "$OK" != "yes" -o "$OK" != "no" ] ; then break fi echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" done [[ $OK = "yes" ]] || fatal "Repeat execution with different parameters." echo "" echononl " Create log directory '$(dirname "$logfile")'.." if [[ ! -d "$(dirname "$logfile")" ]] ; then mkdir "$(dirname "$logfile")" if [[ $? -eq 0 ]]; then echo_ok else echo_failed fi else echo_skipped fi > $logfile 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 fatal "File containing User Info not found!" fi pwd=`pwd` cd /tmp 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 if [[ ! "${test_email}" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]] ; then fatal "The given recipient address (${test_email}) for test run does not look like a correct e-mail address!" fi domains="${test_email##*@}" else domains=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT domain FROM domain WHERE domain != 'ALL' ORDER BY domain\""` fi declare -i num_dom=0; declare -i num_mbox_failed=0; declare -i num_mbox=0; # - Escape '@' sign for use in perl regex # - email_from_regex="$(echo ${email_from//\@/\\@})" for domain in $domains ;do echo -e "\nDOMAIN: $domain\n" | tee -a $logfile if $TEST_RUN ; then local_parts="${test_email%%@*}" else local_parts=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT local_part FROM mailbox WHERE domain = '$domain'\""` 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" if [ "$?" = "0" ]; then num_mbox=num_mbox+1 echo -e "$rc_done" else echo -e "$rc_failed" echo " [ERROR]: Cannot sent userinfo to \"$local_part\@$domain\"!" >> $logfile num_mbox_failed=num_mbox_failed+1 fi done num_dom=num_dom+1 done if ! $TEST_RUN ; then mv "$user_info_file" "${user_info_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 if [ $num_mbox_failed -gt 0 ];then echo -e "\n\tFailed sending mail to $num_mbox_failed mailboxe(s)" >> $logfile echo -e "\n\t\033[31m\033[1mFailed sending mail to $num_mbox_failed mailboxe(s)\033[m\033[m" echo -e "\n\n-- See $logfile for details\n" | tee -a $logfile fi echo cd $pwd clean_up 0