Blastmail with shell script version 3.3.1
#!/bin/bash
##=================================================================##
## file ini berisi perintah untuk mengirimkan email
## File Name : BETAkirim_V3.3.sh
## Created By : Yudi Ariestya Putranto
## email : yudi.ariestya@live.com
## Version : 3.3.1
## Date : April 2013
## Lihat Readme untuk penggunaan lengkap nya
##=================================================================##
Note : tambahkan di belakang script > /dev/null 2>&1 & untuk menjalankan perintah tersebut di background
contoh: kirim -n 'test aja' debug > /dev/null 2>&1&
##*** JANGAN LUPA RUBAH RECIPIENT NYA BRO ##****
VER="3.3.1"
NOW=$(date +"%Y%m%d")
SUBJECT=$2
PENERIMA=$3
PARAMETER=$1
RECIPT="coba"
export DIR_RCPT="/opt/blastmail/recipient"
export DIR_BODY="/opt/blastmail/body"
export DIR_CONT="/opt/blastmail/log"
export DIR_SEND="/opt/blastmail/senditems"
HIT_RECP=$(cat $DIR_RCPT/*.csv | sed 's/ /\n/g' | wc -l)
SUB_REP=$( cat /opt/blastmail/subject |awk -F "," '{print $1}')
ST_DT=$( cat /opt/blastmail/subject |awk -F "," '{print $2}')
GENERAL_ERROR ()
{
clear
echo "Error: $PARAMETER invalid option -- "
echo "Try Help with $0 -help"
echo
exit 0
}
# make sure file exits for reading
function cek_parm
{
if [ ! -f $DIR_BODY/*.html ]; then
echo "File html tidak ada"
exit 1
fi
if [ ! -f $DIR_RCPT/$PENERIMA ]; then
echo "$0: invalid option -- $PENERIMA"
echo "Gunakan bantuan $0 -help"
exit 1
fi
if [ ! -f $DIR_RCPT/*.csv ];then
echo "File Penerima (*.csv) tidak ada"
exit 1
fi
}
function cek_parm2
{
if [ ! -f $DIR_RCPT/$PENERIMA ]; then
echo "Filename $PENERIMA does not exists"
exit 1
fi
}
GENLOG ()
{
HIT_STAT=$(cat $DIR_CONT/counter.log|grep "Status email"|wc -l)
date +"Status email = terkirim : %b %d %H:%M:%S %Y" >> $DIR_CONT/counter.log
echo Total = $HIT_STAT Email >> $DIR_CONT/counter.log
}
REPORT ()
{
for MAILTO in `cat $DIR_RCPT/$RECIPT`
do
export FROM="cs@karir.com"
export SUBJECT="Newsletter Report "
(
echo "To: $MAILTO"
echo "From: $FROM"
echo "Subject: $SUBJECT"
echo
echo "Subject = $SUB_REP"
echo "Start date = $ST_DT"
echo "End date = $NOW"
echo "Newsletter counters = $HIT_STAT emails"
echo
echo
echo Thank You
echo Administrator
) | /usr/sbin/sendmail -t > /dev/null 2>&1 &
done
}
KIRIM ()
{
## JANGAN RUBAH BAGIAN INI, KARENA SUDAH DEFAULT ##
for MAILTO in `cat $DIR_RCPT/$PENERIMA`
do
export FROM="Customer Service karir.com
export BODY="$DIR_BODY/*.html"
(
echo "To: $MAILTO"
echo "From: $FROM"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo
echo '---q1w2e3r4t5--'
echo "Disclaimer -- This is an e-mail from karir.com (R) intended solely for the named addressee(s). It is
confidential and may contain legally privileged information. Therefore, any unauthorized use, disclosure or copying of this
information is strictly prohibited."
) | /usr/sbin/sendmail -t > /dev/null 2>&1 &
GENLOG
done
}
## BATAS AKHIR BARIS YANG TIDAK BOLEH DI EDIT ##
## UNTUK DEBUG PROGRAM ##
#********======#################********======#################********======#################
DEBUG ()
{
for MAILTO in `cat $DIR_RCPT/$RECIPT`
do
NOW=$(date +"%Y%m%d")
hitung=$(cat $DIR_RCPT/*.csv | sed 's/ /\n/g' | wc -l)
export FROM="cs@karir.com"
export SUBJECT="Newsletter Report $NOW (debug mode)"
(
echo "To: $MAILTO"
echo "From: $FROM"
echo "Subject: $SUBJECT"
echo $hitung emails
echo
echo
echo
echo Thank You
echo Administrator
)| /usr/sbin/sendmail -t
done
echo "Email debug report sudah dikirim, terima kasih"; date
}
kirimdebug ()
{
## JANGAN RUBAH BAGIAN INI, KARENA SUDAH DEFAULT ##
for MAILTO in `cat $DIR_RCPT/$RECIPT`
do
hitung2=$(cat $DIR_RCPT/*.csv | sed 's/ /\n/g' | wc -l)
export FROM="Customer Service karir.com
export BODY="$DIR_BODY/*.html" # isi body email yang berupa html
(
echo "To: $MAILTO"
echo "From: $FROM"
echo "Subject: $SUBJECT (DEBUG MODE)"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY # eksekusi body email
echo
echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail -t
done
echo $SUBJECT,$NOW > /opt/blastmail/subject
echo "Email Uji Coba Sudah Dikirim, ada $hitung2 recipient"
}
function archive_data
{
mv $DIR_RCPT/*.html $DIR_SEND/newsletter-body.html # pindahkan file html ke /event setelah pengiriman
mv $DIR_RCPT/*.csv $DIR_SEND/newsletter-recipient.csv
cd $DIR_SEND/; ./rotate.sh 7 newsletter-body.html newsletter-recipient.csv
cd $DIR_CONT ; ./rotate.sh 7 counter.log
}
KILL_PROS ()
{
CEK_KP=$(ps aux | grep kirim |grep -v grep |wc -l)
if [ $CEK_KP -gt 2 ]; then
kill -9 `ps aux | grep kirim |grep -v grep |awk '{print $2}'`
else
echo "Error - tidak ada proses "
fi
}
CEK_STATUS ()
{
stat=$(ps aux | grep kirim | grep -v grep| wc -l)
if [ $stat -ne 0 ]; then
echo "status stoped"
else
echo "status running"
fi
}
#********======#################********======#################********======#################
PARAM ()
{
if [ $PARAMETER = "-k" ]; then
KILL_PROS
elif [ $PARAMETER = "-v" ]; then
echo "Blastmail with Shell Script Version $VER "
elif [ $PARAMETER = "-i" ]; then
echo jumlah penerima adalah $HIT_RECP
elif [ $PARAMETER = "-g" ]; then
DEBUG
elif [ $PARAMETER = "-help" ]; then
cat help | more
elif [ $PARAMETER = "-r" ]; then
REPORT
elif [ $PARAMETER = "-s" ]; then
CEK_STATUS
elif [ $PARAMETER = "-d" ] ; then
clear; cek_parm; kirimdebug; DEBUG
echo $PENERIMA,$NOW > /opt/blastmail/penerima
elif [ $PARAMETER = "-n" ]; then
cek_parm; cek_parm2
KIRIM;
echo "kirim email"
REPORT
# archive_data
else
GENERAL_ERROR
fi
}
PARAM
Comments
Post a Comment