# $Id: mailout,v 1.45 2015/06/29 19:31:56 fulford Exp $
# $Source: /src/merlin/usr/local/etc/RCS/mailout,v $
# $Revision: 1.45 $
# $Date: 2015/06/29 19:31:56 $
# Copyright (c) 2013 Clifford W Fulford.
# Licensed for public use under the LGPL.
# For assistance contact fulford@fulford.net 0709 229 5385
####################################################################
cmd=`basename $0`
syntax="$cmd [-d(ebug)] [-n <#records>] [-t(est) [-v] [-x] <message_file> [-X exclude_file]"
while [ $# -gt 0 ];do
case $1 in
-c) config=$2;shift 2;;
-d) debug=:;set -x; shift;;
-n) r=$2;shift 2;; #set number of records to be used
-t) test=:;shift;;
-v) verbose=: ;shift ;;
-x) xtract=:;shift;;
-X) Xcld=$2;shift 2;;
-*) echo "syntax: $syntax" >&2 ;exit;;
*) cbriansg=$1;shift;;
esac
done
if [ -n "$Xcld" ];then
if [ -f "$Xcld" ];then
[ `wc -m $Xclude` -lt 2 ] &&{
[ "$verbose" ]&&{
echo "$cmd: Nothing useful in $Xcld">&2
}
}
else
echo "$cmd: Can't find $Xcld" >&2
exit 1
fi
fi
config=${config:-"/usr/local/etc/mailout.cf"}
. $config
[ $test ] && {
[ $tstrcpt != /.*@.*\..*/ ]||{
echo "$cmd: test recipient not set! See $config." >&2
exit 1
}
}
[ -n "$cbriansg" ]&& msg=$clmsg
data=${data:-/var/mailout/data.csv} #default data file
debug=${debug:-""}
if [ -n "$exclude" ];then
[ -f $exclude ] || sudo touch $exclude
fi
boundary=FS_`date +%s`.`basename $msg`
head=/home/fulford/fulford/ads/flare/head
msg=${msg:-/var/mailout/msg} #default message
[ $debug ] && echo "message file = $msg" >&2
r=${r:-1} # number of records
txt=${txt:-/var/mailout/txt} #default txt message
test=${test:-""}
email=/tmp/mailout/msg
verbose=${verbose:-""}
xtract=${xtract:-""}
logdir=/var/log/mailout
[ $debug ] && { set >&2;read ;}
# if log directory doesn't exist create it.
[ -d $logdir ] || { sudo mkdir $logdir ;sudo chmod g+w $logdir ;}
log=$logdir/`basename $msg`
# if directory for temporary files doesn't exist create it.
tdir=`dirname $email`
[ -d $tdir ] ||{ mkdir $tdir;sudo chmod g+w $tdir ;}
# if log doesn't exist create it.
[ -f $log ] || touch $log
# check data and message files are readable
if [ ! -r "$data" -o ! -r "$msg" -o ! -r "$txt" ];then
[ ! -r "$data" ] && echo "$cmd: data file $data not found" >&2
[ ! -r "$txt" ] && {
echo "$cmd: message file $txt not found" >&2
echo "Set a text message file in $config " >&2
}
[ ! -r "$msg" ] && {
echo "$cmd: message file $msg not found" >&2
echo "Set a message file in $config or on command line" >&2
}
echo "syntax: $syntax" >&2
exit 1
fi
[ $debug ] && { echo "$data & $msg found">&2; read ;}
gawk -v boundary=$boundary -v r=$r -v test=$test -v verbose=$verbose -v debug=$debug -v xtract=$xtract -v tstrcpt=$tstrcpt -v txt=$txt -F, '{
if($1 !~ /Authority/ && r > 0 && $1 !~ /^#/){
if(debug) print "count = " r >"/dev/stderr"
# assign unmodified mail address to rcpt
rcpt=$5
# quote any single quotes in original name for grep
gsub("\x27","\\\x27",$5)
# Check if recipient opted out
cmd="grep -c "$5" '$exclude' '$Xclude'"
if(debug) print cmd >"/dev/stderr"
print cmd |& ("/bin/sh")
("/bin/sh") |& getline result
close ("/bin/sh")
#If found quit and start next iteration
if(result >= 1){
if(debug) print $5 " found, next" >"/dev/stderr"
next
}
# Check if domain excluded in exclude files
domain=$5
sub(".*@","",domain)
cmd="grep -c ^"domain" '$exclude' '$Xclude'"
if(debug) print cmd >"/dev/stderr"
print cmd |& ("/bin/sh")
("/bin/sh") |& getline result
close ("/bin/sh")
if(result >= 1){
if(verbose) print domain "is excluded">"/dev/stderr"
next
}
#Check if message already sent -c returns number of times found
cmd="grep -c "$5" '$log'"
if(debug) print cmd >"/dev/stderr"
print cmd |& ("/bin/sh")
("/bin/sh") |& getline result
close ("/bin/sh")
#If found quit and start next iteration
if(result >= 1){
if(debug) print $5 " found, next" >"/dev/stderr"
next
}
if(verbose && xtract) print $5
split($2,n," ")
if(n[2] == "")n[2]="Dear Colleague"
#print "n[2]=" n[2]
if(test) rcpt=tstrcpt
if(debug) print rcpt >"/dev/stderr"
system(">'$email'")
close(">'$email'")
while ((getline msg <"'$head'" ) > 0){
# maibrianerge email address into header
gsub("<rcpt>",rcpt,msg)
gsub("<boundary>",boundary,msg)
print msg >>"'$email'"
} close("'$head'")
while ((getline msg <"'$txt'" ) > 0){
# maibrianerge email address into text message
gsub("<boundary>",boundary,msg)
gsub("<firstname>",n[2],msg)
print msg >>"'$email'"
} close("'$txt'")
print "--"boundary >>"'$email'"
print "Content-Type: text/html" >>"'$email'"
print "Content-Transfer-Encoding: 7bit" >>"'$email'"
print "Content-Disposition: inline" >>"'$email'"
while ((getline msg <"'$msg'" ) > 0){
gsub("<firstname>",n[2],msg)
print msg >>"'$email'"
} close("'$msg'")
print "--"boundary"--" >>"'$email'"
#cmd="mailx -s \"Flare support\" -rfrom@mail.address -b fulford@fulford.net "rcpt"<'$email'"
if(debug) cmd="sudo sendmail -vt -ffrom@mail.address -C/etc/mail/sendmail-flare.cf <'$email'"
else cmd="sudo sendmail -t -C/etc/mail/sendmail-flare.cf -ffrom@mail.address <'$email'"
if(debug) print "mailout = "cmd >"/dev/stderr"
if(! xtract) print cmd |"/bin/sh"
retval = close("/bin/sh")
#print "retval = " retval
if(! xtract) print rcpt,systime() >> "'$log'"
if(verbose && ! xtract) print r," ", rcpt
if(debug){ system("read");close("read")}
if(xtract){ print r," ", rcpt
}else{system("/usr/bin/sleep 2")
close("/usr/bin/sleep 2")
}
r=r - 1
}
}' $data
######################################################################
# This program is free software: you can redistribute it and or #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 of #
# the License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# A copy of the Lesser GNU General Public License and the GNU #
# General Public License is available at #
# <http://www.gnu.org/licenses/>. #
######################################################################
Copyright
© 2003-2017
Clifford W Fulford.
Fulford Consulting Ltd.
Regd. Co. 4250037 in England & Wales.
Regd. office 162, Edward Rd. Nottingham NG2 5GF, England, UK.
Related web sites: City Linux | Flare Support | West Bridgford | Fulford Portal | Joan Mary Fulford (Nottingham Writer) | Fulford Gallery | Amharic Interpreter | Arabic Interpreter | Tigrinya Interpreter
The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.