user1409935
user1409935

Reputation: 373

shell script not sending mail accurately using SMTP server

I wrote script to send mail automatically using an SMTP connection but when I execute the script, sometimes it works and sometimes it is not sending mail. Behavior is quite ambiguous.

Environment : Linux Server Fedora 14 
Mailing Client : Lotus Notes 8.5.2

Please find script below.

# Function for sending email
sendemail(){
date=`date '+%d-%m-%Y'`
dbDir=/var/lib/MYSQLBACKUP/$date
dbname='DBNAME'
log_file="${dbDir}/${dbname}_${date}.log"
attached_file="${dbname}_${date}.log"
echo $log_file
echo $attached_file
encoded_log_file=`cat $log_file | openssl base64`
#echo $encoded_log_file
( echo open 172.40.201.31 25
sleep 8
echo helo 172.40.201.31
echo mail from:[email protected]
echo rcpt to:[email protected]
echo data
echo to:[email protected]
echo from:[email protected]
echo "subject: SPARC CQ DB Backup Report : $date :"
echo "MIME-Version: 1.0"
#echo "Content-Type: text/plain; charset=UTF-8"
#echo "Please view attached file"
echo "Content-Type: text/x-log;name="$attached_file""
echo "Content-Disposition:attachment;filename="$attached_file""
echo "Content-Transfer-Encoding: base64"
echo $encoded_log_file
echo $1
sleep 15
echo .
echo ^]
echo quit ) | telnet
echo "status:$?"
echo "Hello done"
}

sendemail

Upvotes: 1

Views: 2585

Answers (2)

tripleee
tripleee

Reputation: 189908

Here's a rewrite using /usr/lib/sendmail. This is not necessarily the correct location for your system, but you should be able to adapt it.

# Function for sending email
sendemail(){
    date=$(date '+%d-%m-%Y')                        # prefer $(...) over `...`
    dbDir=/var/lib/MYSQLBACKUP/$date
    dbname='DBNAME'
    log_file="${dbDir}/${dbname}_${date}.log"
    attached_file="${dbname}_${date}.log"
    echo $log_file
    echo $attached_file
    encoded_log_file=$(openssl base64 < "$log_file")  # notice UUCA fix + quoting
    #echo $encoded_log_file
    # You should configure sendmail to use 172.40.201.31 as your smarthost
    /usr/lib/sendmail -oi -t <<________HERE
to: [email protected]
from: [email protected]
subject: SPARC CQ DB Backup Report : $date :
MIME-Version: 1.0
Content-Type: text/x-log; name="$attached_file"
Content-Disposition: attachment; filename="$attached_file"
Content-Transfer-Encoding: base64
X-Ample: notice empty line between headers and body!    # <-- look

$encoded_log_file
$1
________HERE
    echo "status:$?"
    echo "Hello done"
}

sendemail

Upvotes: 1

Gereon
Gereon

Reputation: 17882

I would recommend using a library or command line program (like mail or mailx) to send mail instead of trying to implement SMTP in a shell script.

Upvotes: 0

Related Questions