mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-10 01:29:26 +08:00
添加邮件通知功能
This commit is contained in:
+36
-1
@@ -1697,7 +1697,7 @@ def getNotifyData(is_parse=False):
|
||||
data = json.loads(notify_data)
|
||||
|
||||
if is_parse:
|
||||
tag_list = ['tgbot']
|
||||
tag_list = ['tgbot', 'email']
|
||||
for t in tag_list:
|
||||
if t in data and 'cfg' in data[t]:
|
||||
data[t]['data'] = json.loads(deDoubleCrypt(t, data[t]['cfg']))
|
||||
@@ -1759,6 +1759,34 @@ def tgbotNotifyTest(app_token, chat_id):
|
||||
return tgbotNotifyHttpPost(app_token, chat_id, msg)
|
||||
|
||||
|
||||
def emailNotifyMessage(data):
|
||||
'''
|
||||
邮件通知
|
||||
'''
|
||||
sys.path.append(os.getcwd() + "/class/plugin")
|
||||
import memail
|
||||
try:
|
||||
if data['smtp_ssl'] == 'ssl':
|
||||
memail.sendSSL(data['smtp_host'], data['smtp_port'],
|
||||
data['username'], data['password'],
|
||||
data['to_mail_addr'], data['subject'], data['content'])
|
||||
else:
|
||||
memail.send(data['smtp_host'], data['smtp_port'],
|
||||
data['username'], data['password'],
|
||||
data['to_mail_addr'], data['subject'], data['content'])
|
||||
return True
|
||||
except Exception as e:
|
||||
print(getTracebackInfo())
|
||||
return False
|
||||
|
||||
|
||||
def emailNotifyTest(data):
|
||||
print(data)
|
||||
data['subject'] = 'MW通知测试'
|
||||
data['content'] = data['mail_test']
|
||||
return emailNotifyMessage(data)
|
||||
|
||||
|
||||
def notifyMessageTry(msg, stype='common', trigger_time=300, is_write_log=True):
|
||||
|
||||
lock_file = getPanelTmp() + '/notify_lock.json'
|
||||
@@ -1796,6 +1824,13 @@ def notifyMessageTry(msg, stype='common', trigger_time=300, is_write_log=True):
|
||||
else:
|
||||
do_notify = tgbotNotifyMessage(
|
||||
t['app_token'], t['chat_id'], msg)
|
||||
|
||||
if 'email' in data and 'enable' in data['email']:
|
||||
if data['email']['enable']:
|
||||
t = data['email']['data']
|
||||
t['subject'] = 'MW通知'
|
||||
t['content'] = msg
|
||||
do_notify = emailNotifyMessage(t)
|
||||
return do_notify
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# coding: utf-8
|
||||
|
||||
import smtplib
|
||||
|
||||
from email import encoders
|
||||
from email.header import Header
|
||||
from email.mime.text import MIMEText
|
||||
from email.utils import parseaddr, formataddr
|
||||
|
||||
|
||||
def _format_addr(s):
|
||||
name, addr = parseaddr(s)
|
||||
return formataddr((Header(name, 'utf-8').encode(), addr))
|
||||
|
||||
|
||||
def send(smtp_host, smtp_port, username, password, to_mail, subject, content):
|
||||
|
||||
smtp = smtplib.SMTP()
|
||||
smtp.connect(smtp_host, port=smtp_port)
|
||||
smtp.login(user=username, password=password)
|
||||
|
||||
msg = MIMEText(content, 'plain', 'utf-8')
|
||||
msg['From'] = _format_addr(username)
|
||||
msg['To'] = _format_addr(to_mail)
|
||||
msg['Subject'] = Header(subject, 'utf-8').encode()
|
||||
|
||||
smtp.sendmail(from_addr=username, to_addrs=to_mail, msg=msg.as_string())
|
||||
return True
|
||||
|
||||
|
||||
def sendSSL(smtp_host, smtp_port, username, password, to_mail, subject, content):
|
||||
|
||||
smtp = smtplib.SMTP_SSL(smtp_host, port=smtp_port)
|
||||
smtp.login(user=username, password=password)
|
||||
|
||||
msg = MIMEText(content, 'plain', 'utf-8')
|
||||
msg['From'] = _format_addr(username)
|
||||
msg['To'] = _format_addr(to_mail)
|
||||
msg['Subject'] = Header(subject, 'utf-8').encode()
|
||||
|
||||
smtp.sendmail(from_addr=username, to_addrs=to_mail, msg=msg.as_string())
|
||||
smtp.quit()
|
||||
return True
|
||||
+192
-1
@@ -382,7 +382,7 @@ function getTgbot(){
|
||||
layer.open({
|
||||
type: 1,
|
||||
area: "500px",
|
||||
title: '配置TgBot配置',
|
||||
title: 'TgBot配置',
|
||||
closeBtn: 1,
|
||||
shift: 5,
|
||||
btn:["确定","关闭","验证"],
|
||||
@@ -401,6 +401,17 @@ function getTgbot(){
|
||||
var pdata = {};
|
||||
pdata['app_token'] = $('input[name="app_token"]').val();
|
||||
pdata['chat_id'] = $('input[name="chat_id"]').val();
|
||||
|
||||
if (pdata['app_token'] == ''){
|
||||
layer.msg('app_token不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['chat_id'] == ''){
|
||||
layer.msg('chat_id不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
$.post('/config/set_notify',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){
|
||||
showMsg(rdata.msg, function(){
|
||||
if (rdata.status){
|
||||
@@ -414,6 +425,17 @@ function getTgbot(){
|
||||
var pdata = {};
|
||||
pdata['app_token'] = $('input[name="app_token"]').val();
|
||||
pdata['chat_id'] = $('input[name="chat_id"]').val();
|
||||
|
||||
if (pdata['app_token'] == ''){
|
||||
layer.msg('app_token不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['chat_id'] == ''){
|
||||
layer.msg('chat_id不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
$.post('/config/set_notify_test',{'tag':'tgbot', 'data':JSON.stringify(pdata)},function(rdata){
|
||||
showMsg(rdata.msg, function(){
|
||||
if (rdata.status){
|
||||
@@ -427,6 +449,175 @@ function getTgbot(){
|
||||
});
|
||||
}
|
||||
|
||||
function getEmailCfg(){
|
||||
var loadT = layer.msg('正在获取邮件配置信息...',{icon:16,time:0,shade: [0.3, '#000']});
|
||||
$.post('/config/get_notify',{},function(data){
|
||||
layer.close(loadT);
|
||||
|
||||
var smtp_host = 'smtp.163.com';
|
||||
var smtp_port = '25';
|
||||
var username = 'admin';
|
||||
var password = '';
|
||||
var to_mail_addr = '';
|
||||
|
||||
var smtp_ssl_no = 'checked';
|
||||
var smtp_ssl_yes = '';
|
||||
|
||||
if (data.status){
|
||||
if (typeof(data['data']['email']) !='undefined'){
|
||||
smtp_host = data['data']['email']['data']['smtp_host'];
|
||||
smtp_port = data['data']['email']['data']['smtp_port'];
|
||||
username = data['data']['email']['data']['username'];
|
||||
password = data['data']['email']['data']['password'];
|
||||
to_mail_addr = data['data']['email']['data']['to_mail_addr'];
|
||||
|
||||
|
||||
var smtp_ssl = data['data']['email']['data']['smtp_ssl'];
|
||||
if (smtp_ssl == 'ssl'){
|
||||
smtp_ssl_no = '';
|
||||
smtp_ssl_yes = 'checked';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
layer.open({
|
||||
type: 1,
|
||||
area: "500px",
|
||||
title: '邮件配置',
|
||||
closeBtn: 1,
|
||||
shift: 5,
|
||||
btn:["确定","关闭","验证"],
|
||||
shadeClose: false,
|
||||
content: "<div class='bt-form pd20'>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>SMTP服务器</span>\
|
||||
<div class='info-r'><input class='bt-input-text' type='text' name='smtp_host' value='"+smtp_host+"' style='width:100%'/></div>\
|
||||
</div>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>SMTP安全</span>\
|
||||
<div class='info-r checkbox'>\
|
||||
<label><input name='smtp_ssl' type='radio' value='' style='margin-right: 4px;' "+smtp_ssl_no+">None</label>\
|
||||
<label><input name='smtp_ssl' type='radio' value='ssl' style='margin-right: 4px;' "+smtp_ssl_yes+">SSL</label>\
|
||||
</div>\
|
||||
</div>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>SMTP端口</span>\
|
||||
<div class='info-r'><input class='bt-input-text' type='text' name='smtp_port' value='"+smtp_port+"' style='width:100%' /></div>\
|
||||
</div>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>用户名</span>\
|
||||
<div class='info-r'><input class='bt-input-text' type='text' name='username' value='"+username+"' style='width:100%' autocomplete='off'/></div>\
|
||||
</div>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>授权码</span>\
|
||||
<div class='info-r'><input class='bt-input-text' type='password' name='password' value='"+password+"' style='width:100%' autocomplete='off'/></div>\
|
||||
</div>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>发送地址</span>\
|
||||
<div class='info-r'><input class='bt-input-text' type='text' name='to_mail_addr' value='"+to_mail_addr+"' style='width:100%' autocomplete='off'/></div>\
|
||||
</div>\
|
||||
<div class='line'>\
|
||||
<span class='tname'>验证测试</span>\
|
||||
<div class='info-r'>\
|
||||
<textarea class='bt-input-text' name='mail_test' style='width:100%; height: 80px; line-height: 20px; padding: 5px 8px;'>验证测试</textarea></div>\
|
||||
</div>\
|
||||
</div>",
|
||||
yes:function(index){
|
||||
var pdata = {};
|
||||
pdata['smtp_host'] = $('input[name="smtp_host"]').val();
|
||||
pdata['smtp_port'] = $('input[name="smtp_port"]').val();
|
||||
pdata['smtp_ssl'] = $('input[name="smtp_ssl"]:checked').val();
|
||||
pdata['username'] = $('input[name="username"]').val();
|
||||
pdata['password'] = $('input[name="password"]').val();
|
||||
pdata['to_mail_addr'] = $('input[name="to_mail_addr"]').val();
|
||||
|
||||
if (pdata['smtp_host'] == ''){
|
||||
layer.msg('SMTP服务器不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['smtp_port'] == ''){
|
||||
layer.msg('SMTP端口不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['username'] == ''){
|
||||
layer.msg('用户名不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['password'] == ''){
|
||||
layer.msg('授权码不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['to_mail_addr'] == ''){
|
||||
layer.msg('发送地址不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
$.post('/config/set_notify',{'tag':'email', 'data':JSON.stringify(pdata)},function(rdata){
|
||||
showMsg(rdata.msg, function(){
|
||||
if (rdata.status){
|
||||
layer.close(index);
|
||||
}
|
||||
},{icon:rdata.status?1:2},2000);
|
||||
});
|
||||
},
|
||||
|
||||
btn3:function(index){
|
||||
var pdata = {};
|
||||
pdata['smtp_host'] = $('input[name="smtp_host"]').val();
|
||||
pdata['smtp_port'] = $('input[name="smtp_port"]').val();
|
||||
pdata['smtp_ssl'] = $('input[name="smtp_ssl"]:checked').val();
|
||||
pdata['username'] = $('input[name="username"]').val();
|
||||
pdata['password'] = $('input[name="password"]').val();
|
||||
pdata['to_mail_addr'] = $('input[name="to_mail_addr"]').val();
|
||||
pdata['mail_test'] = $('textarea[name="mail_test"]').val();
|
||||
|
||||
|
||||
if (pdata['smtp_host'] == ''){
|
||||
layer.msg('SMTP服务器不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['smtp_port'] == ''){
|
||||
layer.msg('SMTP端口不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['username'] == ''){
|
||||
layer.msg('用户名不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['password'] == ''){
|
||||
layer.msg('授权码不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['to_mail_addr'] == ''){
|
||||
layer.msg('发送地址不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pdata['mail_test'] == ''){
|
||||
layer.msg('验证测试不能为空!', {icon:2});
|
||||
return false;
|
||||
}
|
||||
$.post('/config/set_notify_test',{'tag':'email', 'data':JSON.stringify(pdata)},function(rdata){
|
||||
showMsg(rdata.msg, function(){
|
||||
if (rdata.status){
|
||||
layer.close(index);
|
||||
}
|
||||
},{icon:rdata.status?1:2},2000);
|
||||
});
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getPanelSSL(){
|
||||
var loadT = layer.msg('正在获取证书信息...',{icon:16,time:0,shade: [0.3, '#000']});
|
||||
$.post('/config/get_panel_ssl',{},function(cert){
|
||||
|
||||
@@ -161,7 +161,27 @@
|
||||
<span class="set-info c7">Telegram Bot机器人通知【<b style="color:red;">国内可能无法使用</b>】</span>
|
||||
</p>
|
||||
|
||||
<p class="mtb15">
|
||||
<span class="set-tit text-right" title="绑定账号" style="float: left;">邮件通知</span>
|
||||
<input id="cfg_email" name="cfg_email" onclick="setNotifyApi('email',this)" class="btswitch btswitch-ios" type="checkbox" {{data['notify_email_enable']}}>
|
||||
<label class="btswitch-btn ml5" for="cfg_email" style="float: left;margin-top:4px;"></label>
|
||||
<button ype="button" class="btn btn-default btn-xs panel_api_btn" style="vertical-align: middle; margin-left: 10px" onclick="getEmailCfg();">邮件配置</button>
|
||||
<span class="set-info c7">邮件通知</span>
|
||||
</p>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- <div class="title c6 plr15"><h3 class="f16">VIP</h3></div>
|
||||
<div class="setting-con pd15">
|
||||
<p class="mtb15">
|
||||
<span class="set-tit text-right" title="用户登录" style="float: left;">用户登录</span>
|
||||
<input id="cfg_login" name="cfg_login" onclick="" class="btswitch btswitch-ios" type="checkbox">
|
||||
<label class="btswitch-btn ml5" for="cfg_login" style="float: left;margin-top:4px;"></label>
|
||||
<button ype="button" class="btn btn-default btn-xs" style="vertical-align: middle; margin-left: 10px" onclick="">绑定</button>
|
||||
<span class="set-info c7">【<b style="color:red;">请勿使用</b>】(开发中)</span>
|
||||
</p>
|
||||
|
||||
</div> -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user