2016-09-03 11:05:50 +00:00
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.conf import settings
|
2018-04-02 05:19:31 +00:00
|
|
|
from celery import shared_task
|
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
|
|
|
|
|
|
|
|
2018-04-02 05:19:31 +00:00
|
|
|
@shared_task
|
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)
|
|
|
|
|
|
|
|
Also you can ignore the from_mail, unlike django send_mail, from_email is not a require args:
|
|
|
|
|
|
|
|
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]
|
2019-06-14 02:51:18 +00:00
|
|
|
email_from = settings.EMAIL_FROM or settings.EMAIL_HOST_USER
|
|
|
|
args.insert(2, email_from)
|
2016-09-03 11:05:50 +00:00
|
|
|
args = tuple(args)
|
|
|
|
|
2017-12-12 04:19:45 +00:00
|
|
|
try:
|
2019-08-21 12:27:21 +00:00
|
|
|
return send_mail(*args, **kwargs)
|
2017-12-12 04:19:45 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error("Sending mail error: {}".format(e))
|