Linux系統發送郵件
管理服務器時我們經常需要寫一些監測腳本,然后在出問題的時候通過郵件來通知
SMTP
SMTP(Simple Mail Transfer Protocol)簡易郵件傳輸通訊協議
SMTP服務器是遵循SMTP協議的發送郵件服務器,用來發送或中轉你發出的電子郵件。可以理解為就是用來寄信的,而且大多數SMTP服務器也是免費的。
QQ郵箱的SMTP
授權碼作為密碼
mailx
yum install -y mailx
系統指定SMTP服務器
vim /etc/mail.rc
在文件最后插入:
set from=345678910@qq.com
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' 345678910@qq.com
mail命令語法
mail -s "標題" 郵件地址 < 文件
mail -s "test" 345678910@qq.com </etc/hosts
echo "正文" | mail -s "test" 345678910@qq.com
使用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 from=345678910@163.com 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