SENDBLAST with shell script version 4.0

## file ini berisi perintah untuk mengirimkan email                                                                          
## File Name    : SENDBLAST.sh                                                                                                        
## Created By  : Yudi Ariestya Putranto                                                                                      
## email            : yudi.ariestya@live.com                                                                                      
## Version        :  4                                                                                                                        
## Date             : November 2013                                                                                                
## Lihat Readme untuk penggunaan lengka p nya                                                                          

Note: Now this aplication called SENDBLAST

Update Note:
1. Improvement with source script, increase better performance
2. Fix bugs, when sending to yahoo domain, html table unstable
3. Optimalized Engine
4.in this version, we have divided the engine with parameter environment  .comvar (Component Variable) and .authblast (authentication Blastmail)

1. Pure BLASTMAIL Engine

#!/bin/bash
source .comvar
source .authblast

SUBJECT=$2
PENERIMA=$3
PARAMETER=$1

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 ()
   {
HIT_CON=$(cat $DIR_CONT/counter.log|grep "Status email"|wc -l)
ST_DT=$(cat /opt/blastmail/log/counter.log | head -n1 | cut -c 27-46)
ST_DT2=$(cat /opt/blastmail/log/counter.log | tail -2 | head -n1 | cut -c 27-46)
SUB_REP=$(cat /opt/blastmail/subject )
for MAILTO in `cat $DIR_RCPT/$PENERIMA`
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 = $ST_DT2"
echo "Newsletter counters =  $HIT_CON emails"
echo
echo
echo Thank You
echo Administrator
) | /usr/sbin/sendmail -t  
done


   }


KIRIM ()
   {
        ## JANGAN RUBAH BAGIAN INI, KARENA SUDAH DEFAULT ##

        for MAILTO in `cat $DIR_RCPT/*.csv`
        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: text/html "
                 echo 'Content-Type: multipart/mixed' 
echo "Content-Transfer-Encoding: 8bit"
                 echo "Content-Disposition: inline"
                 cat $BODY
                 echo
                ) | /usr/sbin/sendmail -t
                 GENLOG
                        done
   }

DEBUG ()
{
for MAILTO in `cat $DIR_RCPT/$PENERIMA`
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/$PENERIMA` 
do
hitung2=$(cat $DIR_RCPT/*.csv | sed 's/ /\n/g' | wc -l)
export FROM="Customer Service karir.com " # alamat email pengirim
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: text/html "
                 echo 'Content-Type: multipart/mixed'
                 echo "Content-Transfer-Encoding: 8bit"
                 echo "Content-Disposition: inline"
cat $BODY # eksekusi body email
) | /usr/sbin/sendmail -t 
done

echo $SUBJECT > /opt/blastmail/subject
echo "Email Uji Coba Sudah Dikirim, ada $hitung2 recipient" 
}

function archive_data
{
mv $DIR_BODY/*.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
if [ -f $DIR_RCPT/*.query ];then
     mv $DIR_RCPT/*.query  $DIR_SEND/newsletter-query.csv
else
exit 0
fi
}

GET_DATA ()
{
mysql -h $HOSTS -u $usr -p"$pas" < $DIR_QUERY/*.query | awk 'NR!=1 {print $1}' > $DIR_QUERY/$NOW-blastmail.csv
}

#********======#################********======#################********======#################
PARAM ()
{
if [ $PARAMETER = "-k" ]; then
   KILL_PROS 
   elif [ $PARAMETER = "-v" ]; then 
      echo "Blastmail with Shell Script Version $VER "
         elif [ $PARAMETER = "-i" ]; then
  HIT_RECP=$(cat  $DIR_RCPT/*.csv | sed 's/ /\n/g' | wc -l)
   echo jumlah penerima adalah $HIT_RECP 
        elif [ $PARAMETER = "-g" ]; then
         DEBUG
               elif [ $PARAMETER = "-help" ]; then
              cat help | more 
          elif [ $PARAMETER = "-s" ]; then
       CEK_STATUS 
   elif [ $PARAMETER = "-d" ] ; then
              clear; CEK_BODY; CEK_CSV; kirimdebug; DEBUG
                                        elif [ $PARAMETER = "-D" ]; then
                                          clear; GET_DATA; CEK_BODY; CEK_CSV;CEK_QUERY; kirimdebug; DEBUG
 elif [ $PARAMETER = "-N" ]; then
GET_DATA;CEK_CSV;CEK_BODY; CLEAR_LOGS;KIRIM;REPORT
elif [ $PARAMETER = "-n" ]; then
CEK_CSV; CEK_BODY; CLEAR_LOGS; KIRIM; REPORT; archive_data
                    else
              GENERAL_ERROR
fi


}
PARAM

2. COMPONENT VARIABLE 

## file ini berisi perintah untuk mengirimkan email                                                                          
## File Name    : .comvar                                                                                                              
## Created By  : Yudi Ariestya Putranto                                                                                        
## email            : yudi.ariestya@live.com                                                                                      
## Version        :  4                                                                                                                      
## Date             : November 2013                                                                                                
## Lihat Readme untuk penggunaan lengka p nya                                                                          

#!/bin/bash
VER="4"
NOW=$(date +"%Y%m%d")
export DIR_HOME="/opt/blastmail"
export DIR_RCPT="/opt/blastmail/recipient"
export DIR_BODY="/opt/blastmail/body"
export DIR_CONT="/opt/blastmail/log"
export DIR_SEND="/opt/blastmail/senditems"

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
}

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_CSV
{
 HITCSV=$(find $DIR_RCPT/ -name *.csv | wc -l)
 if [ $HITCSV -lt 1 ] || [ $HITCSV -gt 1 ]; then
 echo "Error -- Duplicate or Empty File Recipient  "
 echo "Try Help With --- $0 -help"
 exit 1
 fi
}

function CEK_BODY
{
 HITBODY=$(find $DIR_BODY/ -name *.html | wc -l)
 if [ $HITBODY -lt 1 ] || [ $HITBODY -gt 1 ]; then
 echo "Error -- Duplicate or Empty File Body html  "
 echo "Please Use Help For Details --- $0 -help"
 exit 1
 fi
}

function CEK_QUERY
{
 HITQUERY=$(find $DIR_RCPT/ -name *.query | wc -l)
 if [ $HITQUERY -lt 1 ] || [ $HITQUERY -gt 1 ]; then
 echo "Error -- Duplicate or Empty File QUERY  "
 echo "Please Use Help For Details --- $0 -help"
 exit 1
 fi
}

CLEAR_LOGS ()
{
rm -f /var/log/maillog
/etc/init.d/rsyslog restart > /dev/null 2>&1&
sleep 1
}

3. Authentication BLASTMAIL

## file ini berisi perintah untuk mengirimkan email                                                                          
## File Name    : .authblast                                                                                                          
## Created By  : Yudi Ariestya Putranto                                                                                      
## email            : yudi.ariestya@live.com                                                                                      
## Version        :  4                                                                                                                        
## Date             : November 2013                                                                                                
## Lihat Readme untuk penggunaan lengka p nya                                                                          

#!/bin/bash
export DIR_QUERY="/opt/blastmail/recipient"
HOSTS="127.0.0.1"
usr="your username"
pas="your password"

Comments

Popular posts from this blog

Configure Open Smtp google with Postfix

membuat random password di linux

ISCSI