Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Django Simple asynchronous email backend (Celery 3.1+)
from celery import shared_task
from celery.contrib.methods import task_method
from django.core.mail.backends.smtp import EmailBackend as BaseEmailBackend
class FakeLock(object):
__enter__ = lambda x: None
__exit__ = lambda a, b, c, d: None
class EmailBackend(BaseEmailBackend):
A wrapper that manages the SMTP network connection asynchronously
def __init__(self, *args, **kwargs):
super(EmailBackend, self).__init__(*args, **kwargs)
# fake lock object, because base methods use this
# celery doesn't support base RLock class serialization. Fixing it.
self._lock = FakeLock()
def send_messages(self, email_messages):
self._send_messages.apply_async((self, email_messages))
return len(email_messages)
@shared_task(filter=task_method, name="send_emails")
def _send_messages(self, email_messages):
return super(EmailBackend, self).send_messages(email_messages)
# Path to class in your environment
EMAIL_BACKEND = 'project.portal.backends.asyncemail.EmailBackend'
# Don't forget add backend module to celery configuration, because it is not placed in app.tasks module and need to be defined.
CELERY_IMPORTS = ['project.portal.backends.asyncemail']
# json doesn't support serialization for python objects, only simple types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.