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=[email protected] #郵箱
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 "郵件主題" [email protected]
8.實時查看 Linux 日誌
[root@ruozedata001 ~]# tail -f /var/log/maillog
9.生產上使用
9.1 發郵件不不帶附件
EMAILFROM=[email protected]
EMAILTO=[email protected] # 多個接受者用 , 分割
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}