新增邮箱配置信息测试发送功能

This commit is contained in:
zmister 2022-11-04 15:19:21 +08:00
parent d0988c278c
commit d746d67f80
3 changed files with 88 additions and 5 deletions

View File

@ -24,6 +24,7 @@ urlpatterns = [
path('setting/',views.admin_setting,name="sys_setting"), # 应用设置
path('forget_pwd/',views.forget_pwd,name='forget_pwd'), # 忘记密码
path('send_email_vcode/',views.send_email_vcode,name='send_email_vcode'), # 忘记密码发送邮件验证码
path('send_email_test', views.send_email_test, name='send_email_test'), # 发送测试邮件
path('admin_register_code/',views.admin_register_code,name='register_code_manage'), # 注册邀请码管理
path('check_update/',views.check_update,name='check_update'), # 检测版本更新
path('admin_center/',views.admin_center,name="admin_center"), # 后台管理

View File

@ -297,6 +297,47 @@ def send_email_vcode(request):
return JsonResponse({'status':False,'data':_('方法错误')})
# 测试电子邮箱配置
@superuser_only
@require_http_methods(['POST'])
def send_email_test(request):
smtp_host = request.POST.get('smtp_host','')
send_emailer = request.POST.get('send_emailer','')
smtp_port = request.POST.get('smtp_port','')
username = request.POST.get('smtp_username','')
pwd = request.POST.get('smtp_pwd','')
ssl = True if request.POST.get('smtp_ssl','') == 'on' else False
# print(smtp_host,smtp_port,send_emailer,username,pwd)
msg_from = send_emailer # 发件人邮箱
msg_to = send_emailer # 收件人邮箱
try:
sitename = SysSetting.objects.get(types="basic", name="site_name").value
except:
sitename = "MrDoc"
subject = "{sitename} - 邮箱配置测试".format(sitename=sitename)
content = "此邮件由管理员配置【{sitename}】邮箱信息时发出!".format(sitename=sitename)
msg = MIMEText(content, _subtype='html', _charset='utf-8')
msg['Subject'] = subject
msg['From'] = '{}[{}]'.format(sitename, msg_from)
msg['To'] = msg_to
try:
# print(smtp_host,smtp_port)
if ssl:
s = smtplib.SMTP_SSL(smtp_host, int(smtp_port)) # 发件箱邮件服务器及端口号
else:
s = smtplib.SMTP(smtp_host, int(smtp_port))
s.login(username, pwd)
s.sendmail(from_addr=msg_from, to_addrs=msg_to, msg=msg.as_string())
s.quit()
return JsonResponse({'status':True,'data':_('发送成功')})
except smtplib.SMTPException as e:
logger.error("邮件发送异常:{}".format(repr(e)))
return JsonResponse({'status':False,'data':repr(e)})
except Exception as e:
logger.error("邮件发送异常:{}".format(repr(e)))
return JsonResponse({'status':False,'data':repr(e)})
# 后台管理 - 仪表盘
@superuser_only
def admin_overview(request):

View File

@ -189,7 +189,7 @@
<!-- 邮箱设置 -->
<div class="layui-tab-item">
<form class="layui-form" action="" method="post">
<form class="layui-form" lay-filter="emailConfig" action="" method="post">
{% csrf_token %}
<input type="text" name="type" hidden value="email">
<div class="layui-form-item">
@ -237,6 +237,7 @@
<div class="layui-form-item">
<div class="layui-input-block">
<button class="pear-btn pear-btn-primary pear-btn-sm" lay-submit lay-filter="formDemo">保存邮箱设置</button>
<button class="pear-btn pear-btn-primary pear-btn-sm" type="button" id="checkEmail">验证邮箱设置</button>
</div>
</div>
</form>
@ -310,10 +311,50 @@
$.ajaxSetup({
data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
});
layui.use(['jquery','form','element'],function(){
var $ = layui.jquery,
form = layui.form,
element = layui.element;
var form = layui.form;
var element = layui.element;
var layer = layui.layer;
// 验证邮箱配置
function checkEmailConfig(){
layer.open({
type:1,
title:"验证邮箱配置",
content:"<div style='padding:10px;'>将以现有配置发送一封测试邮件给发件邮箱,以检测邮箱配置是否正确!</div>",
btn:['发送','取消'],
yes:function(){
sendEmailTest();
},
btn2:function(){
layer.closeAll();
}
})
};
// 发送测试邮件
function sendEmailTest(){
var emailData = form.val("emailConfig");
// console.log(emailData)
layer.load()
$.ajax({
url:'{% url "send_email_test" %}',
method:'post',
data:emailData,
success:function(r){
layer.closeAll()
if(r.status){
layer.msg("测试邮件发送成功",{icon:1})
}else{
layer.msg("测试邮件发送失败"+r.data,{icon:2})
}
},
error:function(){
layer.closeAll()
layer.msg("测试邮件发送异常",{icon:2})
}
})
};
$("#checkEmail").click(function(){
checkEmailConfig()
})
</script>
{% endblock %}