zabbix配置STMP使用SSL发送邮件报警
程序员文章站
2024-03-18 13:35:46
...
zabbix配置STMP使用SSL发送邮件报警
安装mail
#CentOS
yum install -y mailx
配置STMP(/etc/mail.rc) 尾行追加
set from=m*******aaa@qq.com smtp=smtp.163.com
set smtp-auth-user=m*******aaa@qq.com
set smtp-auth-password=m*****3
set smtp-auth=login
测试邮件是否能发送
echo "hehe" | mail -s 'hhaa' z*********@163.com
使用SSL/TLS
mkdir -p /root/.certs/
# 创建目录,用来存放证书;
echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/163.crt
# 向163请求证书;
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
# 添加一个SSL证书到证书数据库中;
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
# 添加一个Global 证书到证书数据库中;
certutil -L -d /root/.certs
# 列出目录下证书;
cd /root/.certs/ && certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i 163.crt
# 让证书受信任;
Notice: Trust flag u is set automatically if the private key is present.
# 如上输出为成功;
添加mail.rc配置文件
set ssl-verify=ignore
set nss-config-dir=/root/.certs
编写发送邮件脚本
如果不进行格式转换或者是替换,可以看到结尾带有win的换行符^M,这会导致邮件发不出去而发生554的错误。解决办法有两种分别是dos2unix进行格式转换或是文本处理器替换。
#!/bin/bash
contact=$1
subject=$2
body=/tmp/mailx.log
echo "$3" >$body
/usr/bin/dos2unix $body
#sed -i 's/^M/\n/g' $body && sed -i 's/[[:space:]]//' $body
mail -s "$subject" "$contact" < $body