Skip to content

Messaging

send_templated_mail

Renders and sends an email from a set of template files sharing a common prefix, mirroring the template convention used by django-allauth's AccountAdapter.render_mail so non-authentication emails follow the same pattern as auth emails.

Parameter Type Default Description
template_prefix str — Prefix used to resolve the subject/text/html templates (see below)
context dict — Context passed to all three templates, plus current_site
recipient_list list — List of recipient email addresses
from_email str None Sender address; falls back to settings.DEFAULT_FROM_EMAIL

Templates

Template Purpose
{template_prefix}_subject.txt Required. Rendered, joined to a single line, and stripped to form the subject
{template_prefix}_message.txt Plain-text body
{template_prefix}_message.html HTML body

At least one of the two message templates must exist, otherwise TemplateDoesNotExist is raised. With both, the HTML is attached as an alternative to the text. With only HTML, the email is sent as HTML.

send_templated_mail(
    template_prefix="emails/order_shipped",
    context={"order": order},
    recipient_list=[order.customer.email],
)

This looks for emails/order_shipped_subject.txt, emails/order_shipped_message.txt, and (optionally) emails/order_shipped_message.html.

Note

current_site (via Site.objects.get_current()) is always injected into the template context automatically, alongside whatever you pass in context.

Note

If a request key is in context, it is also passed to the message templates as the template request (for context processors).

build_templated_mail(template_prefix, context, recipient_list, from_email=None, headers=None)

Does everything send_templated_mail does except sending, and returns the EmailMessage. Use it to add attachments or headers, or to send later.

message = build_templated_mail(
    "emails/order_shipped",
    {"order": order},
    [order.customer.email],
    headers={"Reply-To": "help@example.com"},
)
message.send()

Delivery channels

from drf_corekit.messaging import BaseMessagingChannel, MessageDeliveryService

A channel delivers one message through one transport. MessageDeliveryService routes a message to the channels you register, and never lets a failing channel break the code that sent the message.

BaseMessagingChannel

Abstract base class. Implement send:

class SmsChannel(BaseMessagingChannel):
    def send(self, *, recipient, category, context=None):
        sms_client.send(to=recipient, body=render(category, context))
Argument Description
recipient Email address, phone number, device token, ...
category Message category, used to pick the template
context Template context (dict or None)

MessageDeliveryService

Subclass it and fill CHANNEL_MAP. Add the ids of channels handled elsewhere to SKIPPED_CHANNELS (for example an in app notification).

class DeliveryService(MessageDeliveryService):
    CHANNEL_MAP = {"email": EmailChannel, "sms": SmsChannel}
    SKIPPED_CHANNELS = frozenset({"in_app"})

DeliveryService.dispatch(["email", "sms"], user.email, "welcome", {"name": "Abu"})

dispatch(channels, recipient, category, context=None):

  • A channel id missing from CHANNEL_MAP is logged as a warning and skipped.
  • A channel that raises is logged with its traceback; the other channels still run.
  • Nothing is raised to the caller.

Note

A channel is created on every dispatch (channel_class() with no arguments), so keep its __init__ free of required parameters.