mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-07-16 07:56:21 +00:00
fix: Activate user language when sending emails
This commit is contained in:
parent
70b2d28760
commit
84c9333e3f
@ -22,6 +22,7 @@ from common.tasks import send_mail_async
|
||||
from common.utils import get_logger, lazyproperty, is_openssh_format_key, ssh_pubkey_gen
|
||||
from ops.ansible import JMSInventory, DefaultCallback, SuperPlaybookRunner
|
||||
from ops.ansible.interface import interface
|
||||
from users.utils import activate_user_language
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@ -151,12 +152,13 @@ class BaseManager:
|
||||
if not recipients:
|
||||
return
|
||||
print(f"Send report to: {','.join([str(u) for u in recipients])}")
|
||||
|
||||
report = self.gen_report()
|
||||
report = transform(report, cssutils_logging_level="CRITICAL")
|
||||
subject = self.get_report_subject()
|
||||
emails = [r.email for r in recipients if r.email]
|
||||
send_mail_async(subject, report, emails, html_message=report)
|
||||
for user in recipients:
|
||||
with activate_user_language(user):
|
||||
report = self.gen_report()
|
||||
report = transform(report, cssutils_logging_level="CRITICAL")
|
||||
subject = self.get_report_subject()
|
||||
emails = [r.email for r in recipients if r.email]
|
||||
send_mail_async(subject, report, emails, html_message=report)
|
||||
|
||||
def gen_report(self):
|
||||
template_path = self.get_report_template()
|
||||
|
@ -6,6 +6,7 @@ from django.core.mail import send_mail, EmailMultiAlternatives, get_connection
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from common.storage import jms_storage
|
||||
from users.models import User
|
||||
from .utils import get_logger
|
||||
|
||||
logger = get_logger(__file__)
|
||||
@ -48,6 +49,8 @@ def send_mail_async(*args, **kwargs):
|
||||
Example:
|
||||
send_mail_sync.delay(subject, message, recipient_list, fail_silently=False, html_message=None)
|
||||
"""
|
||||
from users.utils import activate_user_language
|
||||
|
||||
if len(args) == 3:
|
||||
args = list(args)
|
||||
args[0] = (settings.EMAIL_SUBJECT_PREFIX or '') + args[0]
|
||||
@ -63,7 +66,10 @@ def send_mail_async(*args, **kwargs):
|
||||
)
|
||||
|
||||
try:
|
||||
return send_mail(connection=get_email_connection(), *args, **kwargs)
|
||||
users = User.objects.filter(email__in=recipient_list).all()
|
||||
for user in users:
|
||||
with activate_user_language(user):
|
||||
send_mail(connection=get_email_connection(), *args, **kwargs)
|
||||
except Exception as e:
|
||||
logger.error("Sending mail error: {}".format(e))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user