天天看點

CentOS下利用mailx發送郵件

CentOS下利用mailx發送郵件

Linux伺服器運維過程中需要監控系統狀況并自動報警,有時有郵件報警的需要,一般Linux發送報警郵件可以通過本地郵箱或外部郵箱伺服器,這裡用最簡單的方法:利用mailx一個小型的郵件發送程式使用外部郵箱即可實作發送郵件功能

1、CentOS伺服器預設安裝了mailx

CentOS下利用mailx發送郵件

若沒有安裝,使用yum install mailx進行安裝

CentOS下利用mailx發送郵件

2、vi編輯mail的配置檔案/etc/mail.rc

set [email protected]

set smtp=smtps://smtp.qq.com:465

set [email protected]

set smtp-auth-password=你的QQ郵箱授權碼

set smtp-auth=login

#set smtp-use-starttls 這裡是不需要配置的,很多地方沒說明,配置了反而會驗證失敗,是以注釋掉;

set ssl-verify=ignore set nss-config-dir=/root/.certs

CentOS下利用mailx發送郵件

QQ郵箱的授權碼并非QQ郵箱密碼

CentOS下利用mailx發送郵件
CentOS下利用mailx發送郵件

關于郵箱授權碼的說明參考官方幫助文檔:

https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001256&&id=28

3、還需要添加郵箱證書到本地

# 建立證書目錄

[root@localhost ~]# mkdir -p /root/.certs/

# 獲驗證書内容

[root@localhost ~]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

# 添加證書到資料庫

[root@localhost ~]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt

[root@localhost ~]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt

# 列出指定目錄下的證書

[root@localhost ~]# certutil -L -d /root/.certs

# 在指令行裡發送郵件會提示“Error in certificate: Peer's certificate issuer is not recognized.”這樣的證書沒有獲得認可的警告提示,需要執行下面指明受信任證書、防報錯的指令

[root@localhost ~]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/qq.crt

CentOS下利用mailx發送郵件

4、發送測試郵件

echo "E-mail Test Content" | mail -s "Alarm Email Test" [email protected]

CentOS下利用mailx發送郵件

5、驗證自己126郵箱是否收到測試郵件,可以看到已經收到測試郵件

CentOS下利用mailx發送郵件

本文參考如下博文實作

https://www.imydl.tech/lnmp/198.html

https://www.jianshu.com/p/b1cdd594c67a