Django Celery Email
django-celery-email is an asynchronous email backend for Django that integrates with Celery to send emails in the background. This offloads email sending from the request-response cycle, improving web application responsiveness. The current version is 3.0.0, focusing on compatibility with modern Django and Python versions.
Common errors
-
ModuleNotFoundError: No module named 'celery'
cause Celery is not installed or not available in the Python environment where django-celery-email is running.fixInstall Celery: `pip install celery` (and optionally a broker like `pip install redis`). -
RuntimeError: No application found. You did not specify an application.
cause This error typically indicates that your Celery application is not correctly defined or discovered, or the Celery worker was started without pointing to your Django project.fixEnsure you have a `celery.py` file in your Django project, and start your Celery worker from your project root: `celery -A your_project_name worker -l info`. -
Emails are not sending, or tasks are stuck in 'PENDING' state.
cause The Celery worker is not running, is misconfigured, or cannot connect to the broker.fixVerify your Celery worker is running (`celery -A your_project_name worker -l info`). Check Celery broker configuration (`CELERY_BROKER_URL`) and ensure the broker service (e.g., Redis server) is running and accessible.
Warnings
- breaking Version 3.0.0 drops support for older Django and Python versions. Django < 3.2 and Python < 3.8 are no longer supported.
- breaking The `recipient_list_split_size` option and related logic have been removed in version 3.0.0. Task arguments have been simplified.
- gotcha django-celery-email requires a fully configured and running Celery setup (broker, result backend, and worker process). It only provides the Django email backend interface, not the Celery infrastructure itself.
- gotcha By default, `CELERY_EMAIL_BACKEND` uses Django's built-in `django.core.mail.backends.smtp.EmailBackend`. If you use a different default backend (e.g., a service-specific backend like SendGrid, Mailgun), you must explicitly configure it.
Install
-
pip install django-celery-email
Imports
- CeleryEmailBackend
from django_celery_email.backends import CeleryEmailBackend
Quickstart
# settings.py
INSTALLED_APPS = [
# ...
'django_celery_email',
# ...
]
# Configure Celery (e.g., using Redis as a broker)
CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL', 'redis://localhost:6379/0')
# Point Django to use CeleryEmailBackend
EMAIL_BACKEND = 'django_celery_email.backends.CeleryEmailBackend'
# Optionally, specify the actual backend Celery should use to send emails
# Defaults to 'django.core.mail.backends.smtp.EmailBackend'
# CELERY_EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Optional: Configure Celery task options
CELERY_EMAIL_TASK_CONFIG = {
'queue': 'celery_email',
'rate_limit': '50/m',
}
# Example usage in an app (e.g., views.py)
from django.core.mail import send_mail
def my_view(request):
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
return HttpResponse('Email scheduled!')