📜  django celery email - Python (1)

📅  最后修改于: 2023-12-03 15:14:43.126000             🧑  作者: Mango

Django Celery Email

Django Celery Email is an open-source library that allows you to send emails asynchronously using Django, Celery, and Redis. This library can be very useful if you need to send bulk emails or if you want to avoid blocking the application while the email is being sent.

Installation

To install Django Celery Email, you need to have Django, Celery, and Redis installed. You can install Django Celery Email using pip:

pip install django-celery-email

After you install it, you need to add it to your INSTALLED_APPS in your Django settings file:

INSTALLED_APPS = (
    ...
    'djcelery_email',
    ...
)

You also need to add Celery Email's configuration to your Django settings file:

CELERY_EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
CELERY_EMAIL_TASK_CONFIG = {
    'max_retries': 3,
    'ignore_result': True,
    'default_retry_delay': 60 * 5,  # 5 minutes.
}
CELERY_EMAIL_TASK_QUEUE = 'email'
CELERY_EMAIL_TASK_EXPIRE = 60 * 60 * 24  # 24 hours.
Sending Emails

After you have everything set up, sending emails asynchronously is very easy. Here's an example:

from django.core.mail import EmailMessage
from djcelery_email.tasks import send_email_message


email = EmailMessage(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},
    cc=['cc@example.com'],
    )

send_email_message.delay(email)

That's it! The send_email_message.delay() function sends the email asynchronously using Celery and Redis.

Conclusion

Django Celery Email is a great library that can help you send emails asynchronously and improve the performance of your Django application. It's easy to install and use, so go ahead and give it a try!