linux 傳送郵件(centos)
阿新 • • 發佈:2018-11-11
1.安裝mailx
安裝mailx:
yum install mailx
vi /etc/mail.rc
set [email protected]
set smtp="smtps://smtp.qq.com:465"
set smtp-auth-user=12345678
set smtp-auth-password=xxxxxxx (郵箱賬號的客戶端授權碼,需要登陸自己的郵箱進行設定,不是郵箱密碼)
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb
測試:
echo test | mailx -v -s "test" [email protected]
2.生成certificate
解決報錯:
使用上面的配置,郵件是完全能夠發出去的,但是傳送郵件的時候會提示 “Error in certificate: Peer’s certificate issuer is not recognized.”提示,執行以下命令生成一個證書即可。
mkdir -p /root/.certs/ echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -L -d /root/.certs
生成完成之後,修改 mail.rc 郵件配置,修改 nss-config-dir 為上面命令生成的 /root/.certs,儲存即可
3.shell指令碼
建立一個shell指令碼如下:
#/bin/bash #author:findyou help(){ echo "eg: $0 [Subject] [address] [content_file] [file]" echo "" exit 1 } sub="the mail" cDate=`date +%Y%m%d` if [ ! -n "$1" ] ; then help else mail_to=$1 echo " Send Mail to ${mail_to}" fi if [ ! -n "$3" ] ; then mail -s 'the mail' ${mail_to}<$2 else mail -s 'the mail' -a $3 ${mail_to}<$2 fi
呼叫:./mail_send.sh [email protected] /opt/send.log