Last active
December 15, 2021 08:27
-
-
Save Quard/7be985f38235e5795cdd9786f38f9c89 to your computer and use it in GitHub Desktop.
The Python Celery Cookbook: Small Tool, Big Possibilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.core.mail import send_mail | |
from django.template import Engine, Context | |
from myproject.celery import app | |
def render_template(template, context): | |
engine = Engine.get_default() | |
tmpl = engine.get_template(template) | |
return tmpl.render(Context(context)) | |
@app.task | |
def send_mail_task(recipients, subject, template, context): | |
send_mail( | |
subject=subject, | |
message=render_template(f'{template}.txt', context), | |
from_email=settings.DEFAULT_FROM_EMAIL, | |
recipient_list=recipients, | |
fail_silently=False, | |
html_message=render_template(f'{template}.html', context) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are right, fixed, thank you