Search code examples
djangoemailsendgriddjango-celerydjango-anymail

Configuring django-celery-email with django-anymail


I am using django-anymail(sendgrid) to send emails in my web app. I would like to let the emails send in asynchronous manner without letting the users wait for some time.So, how can I configure django-celery-email with django-anymail.

Now, my email config. is:

ANYMAIL = {
"SENDGRID_API_KEY": os.environ.get('SENDGRID_API_KEY')
}
EMAIL_BACKEND = "anymail.backends.sendgrid.EmailBackend"

Solution

  • From the django-celery-email readme:

    By default django-celery-email will use Django's builtin SMTP email backend… If you'd like to use another backend, you may set it in CELERY_EMAIL_BACKEND just like you would normally have set EMAIL_BACKEND before you were using Celery.

    So in your settings.py:

    EMAIL_BACKEND = "djcelery_email.backends.CeleryEmailBackend"
    CELERY_EMAIL_BACKEND = "anymail.backends.sendgrid.EmailBackend"  # your setting from before
    

    Also, note that django-celery-email doesn't know about Anymail's extra sending options, like metadata, tags, template_id, envelope_sender, etc. If you use any of those, you'll need to let django-celery-email know about them in your settings:

    CELERY_EMAIL_MESSAGE_EXTRA_ATTRIBUTES = ['metadata', 'tags']  # or whatever you use
    

    (More info in this issue.)