2、阿裏雲ECS發送郵件到騰訊企業郵箱(ECS默認不開啟25端口)
阿裏雲ECS默認禁用25端口導致發郵件失敗。
方法一:
使用shell腳本發送郵件,需要配置mailx
1、安裝軟件
yum install mailx
2、配置
vim /etc/mail.rc在文件最後加上如下內容:
set smtp=smtps://smtp.exmail.qq.com:465 #郵箱服務器地址
set [email protected] #發送郵件的來源
set [email protected] #用戶名
set smtp-auth-password=xxxx #密碼
set nss-config-dir=/etc/pki/nssdb/ #證書位置
set ssl-verify=ignore
set smtp-auth=login
3、配置證書
mkdir -p /etc/pki/nssdb/
cd /etc/pki/nssdb
#生成證書
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne ‘/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p‘ > /etc/pki/nssdb/qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d /etc/pki/nssdb/ -i /etc/pki/nssdb/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d /etc/pki/nssdb/ -i /etc/pki/nssdb/qq.crt
certutil -L -d /etc/pki/nssdb/
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt #認證
4、測試發送郵件
echo "this email come from centos"|mail -v -s "zabbix" [email protected]
告警郵件使用的shell腳本:
#!/bin/bash
#send mail
messages=`echo $3 | tr ‘\r\n‘ ‘\n‘`
subject=`echo $2 | tr ‘\r\n‘ ‘\n‘`
echo "${messages}" | mail -v -s "${subject}" $1 >>/tmp/mailx.log 2>&1
方法二:
直接使用py腳本發送郵件,使用ssl的465端口,簡單快捷。
#!/bin/env python
#coding:utf8
import smtplib
from email.mime.text import MIMEText
from email.utils import parseaddr,formataddr
from email.header import Header
import sys
def _format_addr(s):
name,addr = parseaddr(s)
#將郵件的name轉換成utf-8格式,addr如果是unicode,則轉換utf-8輸出,否則直接輸出addr
return formataddr((Header(name,‘utf-8‘).encode(),addr.encode("utf-8") if isinstance(addr,unicode) else addr))
def send_mail(to_list,subject,content):
fromaddr = "[email protected]"
msg = MIMEText(content, ‘plain‘, ‘utf-8‘)
msg[‘Subject‘] = subject
msg[‘From‘] = _format_addr(u‘線上監控平臺告警<%s>‘%fromaddr)
msg[‘to‘] = to_list
try:
server = smtplib.SMTP_SSL(mail_host,465)
server.login(mail_user,mail_pass)
server.sendmail(fromaddr,to_list,msg.as_string())
server.close()
return True
except Exception,e:
print str(e)
return False
if __name__ == "__main__":
#smtp信息
mail_host = ‘smtp.exmail.qq.com‘
mail_port = ‘465‘
mail_user = ‘ca‘
mail_pass = ‘Cb‘
send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
告警郵件內容
參考鏈接:
https://www.cnblogs.com/abkn/p/9720143.html
https://blog.csdn.net/qq32712784/article/details/48582945
2、阿裏雲ECS發送郵件到騰訊企業郵箱(ECS默認不開啟25端口)