mirror of https://github.com/jumpserver/jumpserver
Merge pull request #3898 from jumpserver/dev_email
[Update] 修改测试邮件后,配置获取不稳定的Bug(显示给settings赋值导致)pull/3899/head
commit
93bf15adc9
|
@ -7,7 +7,7 @@ from smtplib import SMTPSenderRefused
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
from rest_framework.views import Response, APIView
|
from rest_framework.views import Response, APIView
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail, get_connection
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
|
@ -35,18 +35,33 @@ class MailTestingAPI(APIView):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
|
email_host = serializer.validated_data['EMAIL_HOST']
|
||||||
|
email_port = serializer.validated_data['EMAIL_PORT']
|
||||||
|
email_host_user = serializer.validated_data["EMAIL_HOST_USER"]
|
||||||
|
email_host_password = serializer.validated_data['EMAIL_HOST_PASSWORD']
|
||||||
email_from = serializer.validated_data["EMAIL_FROM"]
|
email_from = serializer.validated_data["EMAIL_FROM"]
|
||||||
email_recipient = serializer.validated_data["EMAIL_RECIPIENT"]
|
email_recipient = serializer.validated_data["EMAIL_RECIPIENT"]
|
||||||
email_host_user = serializer.validated_data["EMAIL_HOST_USER"]
|
email_use_ssl = serializer.validated_data['EMAIL_USE_SSL']
|
||||||
for k, v in serializer.validated_data.items():
|
email_use_tls = serializer.validated_data['EMAIL_USE_TLS']
|
||||||
if k.startswith('EMAIL'):
|
|
||||||
setattr(settings, k, v)
|
# 设置 settings 的值,会导致动态配置在当前进程失效
|
||||||
|
# for k, v in serializer.validated_data.items():
|
||||||
|
# if k.startswith('EMAIL'):
|
||||||
|
# setattr(settings, k, v)
|
||||||
try:
|
try:
|
||||||
subject = "Test"
|
subject = "Test"
|
||||||
message = "Test smtp setting"
|
message = "Test smtp setting"
|
||||||
email_from = email_from or email_host_user
|
email_from = email_from or email_host_user
|
||||||
email_recipient = email_recipient or email_from
|
email_recipient = email_recipient or email_from
|
||||||
send_mail(subject, message, email_from, [email_recipient])
|
connection = get_connection(
|
||||||
|
host=email_host, port=email_port,
|
||||||
|
uesrname=email_host_user, password=email_host_password,
|
||||||
|
use_tls=email_use_tls, use_ssl=email_use_ssl,
|
||||||
|
)
|
||||||
|
send_mail(
|
||||||
|
subject, message, email_from, [email_recipient],
|
||||||
|
connection=connection
|
||||||
|
)
|
||||||
except SMTPSenderRefused as e:
|
except SMTPSenderRefused as e:
|
||||||
resp = e.smtp_error
|
resp = e.smtp_error
|
||||||
if isinstance(resp, bytes):
|
if isinstance(resp, bytes):
|
||||||
|
|
Loading…
Reference in New Issue