1.QQ邮箱开启SMTP并获取授权码
进入QQ邮箱,选择设置-账户,开启如图两个SMTP,并获取授权码
2.关闭sendmail服务
[root@ruozedata001 ~]# service sendmail stop
[root@ruozedata001 ~]# chkconfig sendmail off
3.修改/etc/postfix/main.cf
[root@ruozedata001 ~]# vim /etc/postfix/main.cf
# 默认为 inet_interfaces = localhost,修改成all
inet_interfaces = all
4.启动postfix服务
[root@ruozedata001 ~]# service postfix start
[root@ruozedata001 ~]# chkconfig postfix on
[root@ruozedata001 ~]# postfix check
[root@ruozedata001 ~]# systemctl status postfix
# 启动成功输出
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2019-08-24 15:59:32 CST; 3h 4min ago
Main PID: 1965 (master)
CGroup: /system.slice/postfix.service
├─1965 /usr/libexec/postfix/master -w
├─1967 qmgr -l -t unix -u
└─3545 pickup -l -t unix -u
Aug 24 15:59:31 ruozedata001 systemd[1]: Starting Postfix Mail Transport Agent...
Aug 24 15:59:32 ruozedata001 postfix/postfix-script[1963]: starting the Postfix mail system
Aug 24 15:59:32 ruozedata001 postfix/master[1965]: daemon started -- version 2.10.1, configurat...ix
Aug 24 15:59:32 ruozedata001 systemd[1]: Started Postfix Mail Transport Agent.
Hint: Some lines were ellipsized, use -l to show in full.
5.创建认证
[hadoop@ruozedata001 ~]$ mkdir -p ~/.certs/
[hadoop@ruozedata001 ~]$ echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE -/,/-END [hadoop@ruozedata001 ~]$ CERTIFICATE-/p' > ~/.certs/qq.crt
[hadoop@ruozedata001 ~]$ certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[hadoop@ruozedata001 ~]$ certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[hadoop@ruozedata001 ~]$ certutil -L -d ~/.certs
[hadoop@ruozedata001 ~]$ cd ~/.certs
[hadoop@ruozedata001 ~]$ certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
# 成功输出
Notice: Trust flag u is set automatically if the private key is present.
6.配置邮件发送者
[root@ruozedata001 ~]# vim /etc/mail.rc
# 底部添加发送着信息
set from=1115346515@qq.com #邮箱
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=1115346515 #QQ号
set smtp-auth-password=********** #刚才申请的授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/home/hadoop/.certs # 认证文件路径,注意根据自己用户的路径填写不要盲目复制
7.测试
[hadoop@ruozedata001 ~]$ echo "邮件正文" | mail -s "邮件主题" 1115346515@qq.com
8.实时查看 Linux 日志
[root@ruozedata001 ~]# tail -f /var/log/maillog
9.生产上使用
9.1 发邮件不不带附件
EMAILFROM=503757851@qq.com
EMAILTO=503757851@qq.com # 多个接受者用 , 分割
echo -e "`date "+%Y-%m-%d %H:%M:%S"` : The current running $JOB_NAME job num is $RUNNINGNUM in 192.168.137.201 ......" | mail \
-r "From: alertAdmin <${EMAILFROM}>" \
-s "Warn: Skip the new $JOB_NAME spark job." ${EMAILTO}
9.2 发邮件带附件
echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the fail sql attachement."|mailx \
-r "From: alertAdmin <${EMAILFROM}>" \
-a error.log \
-s "Critical:KSSH fail sql." ${EMAILTO}