shell脚本无法使用SMTP服务器准确发送邮件

时间:2013-01-18 06:16:17

标签: linux shell smtp

我编写了脚本以使用SMTP连接自动发送邮件,但是当我执行脚本时,有时它会工作,有时它不会发送邮件。行为很模糊。

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

请在下面找到脚本。

# 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

2 个答案:

答案 0 :(得分:1)

这是使用/usr/lib/sendmail的重写。这不一定是您系统的正确位置,但您应该能够对其进行调整。

# 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

答案 1 :(得分:0)

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