简体   繁体   中英

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:Pratik.Vyas@gmail.com
echo rcpt to:Pratik.Vyas@gmail.com
echo data
echo to:Pratik.Vyas@gmail.com
echo from:Pratik.Vyas@gmail.com
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

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: Pratik.Vyas@gmail.com
from: Pratik.Vyas@gmail.com
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

我建议使用库或命令行程序(例如mailmailx )发送邮件,而不要尝试在Shell脚本中实现SMTP。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM