2021-12-03 07:50:04 +00:00
|
|
|
import os
|
|
|
|
|
2018-04-02 05:19:31 +00:00
|
|
|
from celery import shared_task
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.conf import settings
|
2024-01-02 09:00:15 +00:00
|
|
|
from django.core.mail import send_mail, EmailMultiAlternatives, get_connection
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-11-07 07:10:46 +00:00
|
|
|
import jms_storage
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2017-12-12 04:19:45 +00:00
|
|
|
from .utils import get_logger
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2016-11-11 01:48:47 +00:00
|
|
|
|
|
|
|
|
2024-01-02 09:00:15 +00:00
|
|
|
def get_email_connection(**kwargs):
|
|
|
|
email_backend_map = {
|
|
|
|
'smtp': 'django.core.mail.backends.smtp.EmailBackend',
|
|
|
|
'exchange': 'jumpserver.rewriting.exchange.EmailBackend'
|
|
|
|
}
|
|
|
|
return get_connection(
|
|
|
|
backend=email_backend_map.get(settings.EMAIL_PROTOCOL), **kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-24 09:59:32 +00:00
|
|
|
def task_activity_callback(self, subject, message, recipient_list, *args, **kwargs):
|
2023-02-17 09:14:53 +00:00
|
|
|
from users.models import User
|
|
|
|
email_list = recipient_list
|
|
|
|
resource_ids = list(User.objects.filter(email__in=email_list).values_list('id', flat=True))
|
2023-02-23 02:46:59 +00:00
|
|
|
return resource_ids,
|
2023-02-17 09:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task(verbose_name=_("Send email"), activity_callback=task_activity_callback)
|
2016-09-03 11:05:50 +00:00
|
|
|
def send_mail_async(*args, **kwargs):
|
|
|
|
""" Using celery to send email async
|
|
|
|
|
|
|
|
You can use it as django send_mail function
|
|
|
|
|
|
|
|
Example:
|
|
|
|
send_mail_sync.delay(subject, message, from_mail, recipient_list, fail_silently=False, html_message=None)
|
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
Also, you can ignore the from_mail, unlike django send_mail, from_email is not a required args:
|
2016-09-03 11:05:50 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
send_mail_sync.delay(subject, message, recipient_list, fail_silently=False, html_message=None)
|
|
|
|
"""
|
|
|
|
if len(args) == 3:
|
|
|
|
args = list(args)
|
2020-09-02 01:57:28 +00:00
|
|
|
args[0] = (settings.EMAIL_SUBJECT_PREFIX or '') + args[0]
|
2021-12-03 07:50:04 +00:00
|
|
|
from_email = settings.EMAIL_FROM or settings.EMAIL_HOST_USER
|
|
|
|
args.insert(2, from_email)
|
2023-09-27 02:31:44 +00:00
|
|
|
|
|
|
|
args = tuple(args)
|
2017-12-12 04:19:45 +00:00
|
|
|
try:
|
2024-01-02 09:00:15 +00:00
|
|
|
return send_mail(connection=get_email_connection(), *args, **kwargs)
|
2017-12-12 04:19:45 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error("Sending mail error: {}".format(e))
|
2021-12-03 07:50:04 +00:00
|
|
|
|
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
@shared_task(verbose_name=_("Send email attachment"), activity_callback=task_activity_callback)
|
2021-12-03 07:50:04 +00:00
|
|
|
def send_mail_attachment_async(subject, message, recipient_list, attachment_list=None):
|
|
|
|
if attachment_list is None:
|
|
|
|
attachment_list = []
|
|
|
|
from_email = settings.EMAIL_FROM or settings.EMAIL_HOST_USER
|
2023-07-13 13:50:17 +00:00
|
|
|
subject = (settings.EMAIL_SUBJECT_PREFIX or '') + subject
|
2021-12-03 07:50:04 +00:00
|
|
|
email = EmailMultiAlternatives(
|
|
|
|
subject=subject,
|
|
|
|
body=message,
|
|
|
|
from_email=from_email,
|
2024-01-02 09:00:15 +00:00
|
|
|
to=recipient_list,
|
|
|
|
connection=get_email_connection(),
|
2021-12-03 07:50:04 +00:00
|
|
|
)
|
|
|
|
for attachment in attachment_list:
|
|
|
|
email.attach_file(attachment)
|
|
|
|
os.remove(attachment)
|
|
|
|
try:
|
|
|
|
return email.send()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Sending mail attachment error: {}".format(e))
|
2023-11-07 07:10:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task(verbose_name=_('Upload session replay to external storage'))
|
|
|
|
def upload_backup_to_obj_storage(recipient, upload_file):
|
|
|
|
logger.info(f'Start upload file : {upload_file}')
|
|
|
|
remote_path = os.path.join('account_backup', os.path.basename(upload_file))
|
|
|
|
storage = jms_storage.get_object_storage(recipient.config)
|
|
|
|
ok, err = storage.upload(src=upload_file, target=remote_path)
|
|
|
|
if not ok:
|
|
|
|
logger.error(f'upload {upload_file} failed, error: {err}')
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
os.remove(upload_file)
|
|
|
|
except Exception as e:
|
|
|
|
print(f'remove upload file : {upload_file} error: {e}')
|