mirror of https://github.com/jumpserver/jumpserver
[Bugfix] 解决发送邮件重启的问题
parent
757a31a52f
commit
69e5ab438a
|
@ -21,23 +21,13 @@ class MailTestingAPI(APIView):
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
email_host_user = serializer.validated_data["EMAIL_HOST_USER"]
|
email_host_user = serializer.validated_data["EMAIL_HOST_USER"]
|
||||||
kwargs = {
|
for k, v in serializer.validated_data.items():
|
||||||
"host": serializer.validated_data["EMAIL_HOST"],
|
if k.startswith('EMAIL'):
|
||||||
"port": serializer.validated_data["EMAIL_PORT"],
|
setattr(settings, k, v)
|
||||||
"username": serializer.validated_data["EMAIL_HOST_USER"],
|
|
||||||
"password": serializer.validated_data["EMAIL_HOST_PASSWORD"],
|
|
||||||
"use_ssl": serializer.validated_data["EMAIL_USE_SSL"],
|
|
||||||
"use_tls": serializer.validated_data["EMAIL_USE_TLS"]
|
|
||||||
}
|
|
||||||
connection = get_connection(timeout=5, **kwargs)
|
|
||||||
try:
|
try:
|
||||||
connection.open()
|
subject = "Test"
|
||||||
except Exception as e:
|
message = "Test smtp setting"
|
||||||
return Response({"error": str(e)}, status=401)
|
send_mail(subject, message, email_host_user, [email_host_user])
|
||||||
|
|
||||||
try:
|
|
||||||
send_mail("Test", "Test smtp setting", email_host_user,
|
|
||||||
[email_host_user], connection=connection)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"error": str(e)}, status=401)
|
return Response({"error": str(e)}, status=401)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ from django.core.mail import send_mail
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
from .utils import get_logger
|
from .utils import get_logger
|
||||||
|
from .models import Setting
|
||||||
|
|
||||||
|
|
||||||
logger = get_logger(__file__)
|
logger = get_logger(__file__)
|
||||||
|
@ -21,6 +22,10 @@ def send_mail_async(*args, **kwargs):
|
||||||
Example:
|
Example:
|
||||||
send_mail_sync.delay(subject, message, recipient_list, fail_silently=False, html_message=None)
|
send_mail_sync.delay(subject, message, recipient_list, fail_silently=False, html_message=None)
|
||||||
"""
|
"""
|
||||||
|
configs = Setting.objects.filter(name__startswith='EMAIL')
|
||||||
|
for config in configs:
|
||||||
|
setattr(settings, config.name, config.cleaned_value)
|
||||||
|
|
||||||
if len(args) == 3:
|
if len(args) == 3:
|
||||||
args = list(args)
|
args = list(args)
|
||||||
args[0] = settings.EMAIL_SUBJECT_PREFIX + args[0]
|
args[0] = settings.EMAIL_SUBJECT_PREFIX + args[0]
|
||||||
|
|
Loading…
Reference in New Issue