利用linux调用sendmail模块方式发邮件比程序写的调用mail函数的速度要快多倍,例如发送每封促销邮件大约15秒左右,发送代码我已写成一个shell文件,通过设置要执行文件的路径便可轻松发送邮件,前提需要配置sendmail模块
#!/bin/sh
mysql -u admin -D -padmin </backup/tmp/script.sql >/backup/tmp/result #执行sql语句读取客户email地址并将结果生成result文件里
mkdir /backup/tmp/emailpath
chmod 0777 /backup/tmp/emailpath
awk -F '[\t]' '{print $1}' result >/backup/tmp/emailpath/emailAddress #将客户邮箱地址生成到指定的文件里,便于读取转为数组
awk -F '[\t]' '{print $2}' result >/backup/tmp/emailpath/emailBakAddress #将客户备份邮箱地址生成到指定的文件里,便于读取转为数组
EMAILADDPATH="/backup/tmp/emailpath/emailAddress" #客户邮箱地址文件
EMAILADDBAKPATH="/backup/tmp/emailpath/emailBakAddress" #客户备份邮件地址文件
EMAILBODY="/backup/tmp/chritmas.html" #邮件内容文件
SUBJECT="Merry Christmas From " #设置邮件标题
FROM="Milky Way Jewelry - <sales@>"
echo "读取客户email"
j=0
while read line
do
let j++
emailAddressArr[$j]=$line
done < ${EMAILADDPATH}
#echo ${emailAddressArr[@]}
echo "读取客户备份email"
k=0
while read line
do
let k++
emailBakAddressArr[$k]=$line
#echo ${emailBakAddressArr[$k]}
done < ${EMAILADDBAKPATH}
#echo ${emailBakAddressArr[@]}
#统计共多少个客户
len=${#emailAddressArr[*]}
let total=len-1;
echo 共${total}位客户:
l=1
while [ $l -lt $len ]
do
let l++
toAdd="-i To:${emailAddressArr[$l]}"
if [ ["${emailBakAddressArr[$l]}" != -n] ];then
if [ "${emailBakAddressArr[$l]}" != NULL ];then
ccAdd="-i Cc:${emailBakAddressArr[$l]}"
fi
fi
let nextlmail=l-1
echo 正在发送第$nextlmail封邮件
cat ${EMAILBODY} |formail -i "From:${FROM}" -i "Sender:sales@" $ccAdd -i "Reply-To:sales@" $toAdd -i "MIME-Version:1.0" -i "Content-type:text/html;charset=uft-8" -i "Subject:${SUBJECT}"|/usr/sbin/sendmail -oi ${emailAddressArr[$l]}
echo 第$nextlmail封邮件发送成功!
done
以上为shell程序
注意 客户邮箱文件与客户备份邮箱需要通过自动执行上面的连接生成文件.