1. 程式人生 > 其它 >Linux系統發郵件

Linux系統發郵件

Linux系統傳送郵件

管理伺服器時我們經常需要寫一些監測指令碼,然後在出問題的時候通過郵件來通知

SMTP

SMTP(Simple Mail Transfer Protocol)簡易郵件傳輸通訊協議

SMTP伺服器是遵循SMTP協議的傳送郵件伺服器,用來發送或中轉你發出的電子郵件。可以理解為就是用來寄信的,而且大多數SMTP伺服器也是免費的。

QQ郵箱的SMTP

授權碼作為密碼

mailx

yum install -y mailx

系統指定SMTP伺服器

vim /etc/mail.rc

在檔案最後插入:

set [email protected]
set smtp=smtp.qq.com  
set smtp-auth-user=345678910
set smtp-auth-password=授權碼
set smtp-auth=login  

from是你設定stmp服務的郵箱地址,也就是用來發送郵件的郵箱地址
smtp是發生的外部smtp伺服器的地址,如qq的smtp.qq.com
smtp-auth-user是外部smtp伺服器認證的使用者名稱
smtp-auth-password是外部smtp伺服器認證的使用者密碼(授權碼)
smtp-auth是郵件認證的方式

傳送郵件測試

echo ‘test’ | mail -s 'test' [email protected]

mail命令語法

mail -s "標題" 郵件地址 < 檔案

mail -s "test" [email protected] </etc/hosts

echo "正文" | mail -s "test" [email protected]

使用465埠傳送郵件

阿里雲回覆:

  由於國際與國內均對垃圾郵件進行嚴格管控,我國《網際網路資訊服務管理辦法》、《中國網際網路協會反垃圾郵件規範》均對垃圾郵件進行說明與管理規範。 鑑於伺服器25埠被大量垃圾郵件充斥,嚴重影響廣大使用者正常使用。為了共同維護良好的網路環境,自即日起阿里雲新購伺服器不再提供25埠郵件服務, 建議您嘗試使用465加密埠傳送郵件。

建立證書:

mkdir /root/.certs
echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /root/.certs/163.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs -i /root/.certs/163.crt
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d /root/.certs/./ -i /root/.certs/163.crt
certutil -L -d /root/.certs

mail.rc:

set [email protected]
set smtp=smtps://smtp.163.com:465
set smtp-auth-user=345678910
set smtp-auth-password=password
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs