使用 django-anymail 配置 django-celery-email

Configuring django-celery-email with django-anymail

我正在使用 django-anymail(sendgrid) 在我的网络应用程序中发送电子邮件。我想让电子邮件以异步方式发送,而不让用户等待一些 time.So,如何使用 django-anymail 配置 django-celery-email。

现在,我的电子邮件配置。是:

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

来自 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.

所以在你的 settings.py:

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

此外,请注意 django-celery-email 不知道 Anymail 的 extra sending options,例如 metadatatagstemplate_idenvelope_sender等等。如果你使用其中任何一个,你需要在你的设置中让 django-celery-email 知道它们:

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

(更多信息见 this issue。)