每次在Linux下写脚本都有发送邮件的功能 ,但是在Linux 下的配置老是忘记,所以这次写了个小脚本,记录下,下次要配置时运行一下脚本就行了。这里使用的是QQ邮箱.

测试环境:CentOS 6.5 x86_64


脚本内容如下:

#!/bin/bash

. /etc/init.d/functions


if [ $# -ne 0 ];then
    echo "Usage: sh `basename $0`"
    exit 1 
fi

frommail='147220591@qq.com'
smtp_host='smtp.qq.com'
smtp_auth_user='147220591@qq.com'
smpt_auth_password='mdkuasfhnjbrbhdj'
certs_dir=/root/.certs
mail_conf=/etc/mail.rc


content=`cat $mail_conf |grep 'smtp-auth-user'`
if [ "x$content" != "x"  ];then
   action  "'$mail_conf' 文件已经配置" /bin/true
   exit 1
fi


cat >> $mail_conf <<EOF 
set from=$frommail
set smtp=$smtp_host
set smtp-auth-user=$smtp_auth_user
set smtp-auth-password=$smpt_auth_password
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=$certs_dir
EOF

[ $? -eq 0  ] && action "配置$mail_conf文件" /bin/true 

[ ! -d $certs_dir ] && mkdir -p $certs_dir
echo -n | openssl s_client -connect $smtp_host:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $certs_dir/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d $certs_dir -i $certs_dir/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d $certs_dir -i $certs_dir/qq.crt
certutil -L -d $certs_dir

[ $? -eq 0  ] && action "生成证书" /bin/true


在root用户下执行:

sh auto_mail_configure.sh

wKioL1hGb2PSz5fPAABrjtSQBow777.png-wh_50

执行完成


接下来执行下面的命令进行测试:

echo  hello word | mailx -v -s " title" 737735250@qq.com

wKioL1hGb9GgFR7CAADbxQeu7_U024.png-wh_50

查看有没有收到邮件:

wKiom1hGcDbgrWAWAAE8XplqvXg043.png-wh_50