Skip to content

Instantly share code, notes, and snippets.

@Quard
Last active December 15, 2021 08:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quard/7be985f38235e5795cdd9786f38f9c89 to your computer and use it in GitHub Desktop.
Save Quard/7be985f38235e5795cdd9786f38f9c89 to your computer and use it in GitHub Desktop.
The Python Celery Cookbook: Small Tool, Big Possibilities
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)
)
@Quard
Copy link
Author

Quard commented Jul 18, 2019

You are right, fixed, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment