A few years ago, in “You want to compose emails, right?”, I argued that an email is a frontend like any web page. The only difference is the transport: HTTP for the page, SMTP for the email. If it’s a frontend, we can use ViewModel Composition to gather the data it needs.

After that article, a reader asked a follow-up question: they were using SendGrid templates and wanted to know how the component that fills and sends the email is hosted. It’s a fair question, and the 2021 article didn’t answer it.

Let’s answer it with an example: the order confirmation email. Customers place an order, and they receive an email with the list of products they bought, the order total, the shipping address, and the estimated delivery date.

Remember mail merge?

Microsoft Word has had a feature called mail merge for decades. Someone designs a letter with placeholders, such as «FirstName» and «Address», and Word fills them with rows from a data source, for example, a spreadsheet. The person designing the letter doesn’t own the spreadsheet, and the spreadsheet doesn’t know anything about the letter.

That’s precisely the split we need. Who owns what in the order confirmation email?

  • Marketing owns the template: the wording, the layout, the branding, and the decision of when an email is worth sending. It also owns product names, as I discussed in “Back to Basics: who owns the data?”.
  • Sales owns the order: the items, their quantities and prices, and the total.
  • Shipping owns the shipping address and the estimated delivery date.
  • Customers owns the customer’s name and email address.

Marketing owns the document, but almost none of the data. If marketing reached into other services’ databases to fill the template, every change to those databases would break the email. If, instead, Sales and Shipping started building emails, wording changes would require releasing services unrelated to marketing.

What marketing needs is a data source that someone else fills. That’s what a composed API is.

Composing the model

The composition gateway exposes a route dedicated to the email: /emails/order-confirmation/{orderId}. Why not reuse the order details page route? Because the email isn’t the page. It needs different data, for example, the delivery estimate, and it’s requested by a back-end component rather than by the customer, which affects authorization. A dedicated route allows each service to decide what to contribute to the email without affecting the page.

Each service contributes its slice with a composition handler. Sales loads the order items and raises an event, so that other services can enrich them:

record OrderItemsLoaded(IDictionary<int, dynamic> ItemsByProductId);

[CompositionHandler]
class SalesOrderConfirmationHandler(SalesDb db, IHttpContextAccessor httpContextAccessor)
{
    [HttpGet("/emails/order-confirmation/{orderId}")]
    public async Task Handle(int orderId)
    {
        var request = httpContextAccessor.HttpContext.Request;
        var context = request.GetCompositionContext();
        var vm = request.GetComposedResponseModel();

        var order = await db.GetOrder(orderId);
        var itemsByProductId = new Dictionary<int, dynamic>();
        foreach (var line in order.Lines)
        {
            dynamic item = new ExpandoObject();
            item.ProductId = line.ProductId;
            item.Quantity = line.Quantity;
            item.Price = line.Price;
            itemsByProductId[line.ProductId] = item;
        }

        await context.RaiseEvent(new OrderItemsLoaded(itemsByProductId));

        vm.Items = itemsByProductId.Values.ToArray();
        vm.Total = order.Total;
    }
}

Marketing subscribes to the event and adds product names:

class MarketingOrderItemsHandler(MarketingDb db) : ICompositionEventsHandler<OrderItemsLoaded>
{
    public async Task Handle(OrderItemsLoaded @event, HttpRequest request)
    {
        var names = await db.GetProductNames(@event.ItemsByProductId.Keys);
        foreach (var (productId, item) in @event.ItemsByProductId)
        {
            item.ProductName = names[productId];
        }
    }
}

If the event-based approach to lists looks unfamiliar, I covered it in “Into the darkness of ViewModel Lists Composition”.

Shipping and Customers add their properties in the same way, with handlers bound to the same route. The composed result is a JSON document like the following:

{
  "items": [
    { "productId": 42, "quantity": 2, "price": 19.90, "productName": "Coffee mug" },
    { "productId": 7, "quantity": 1, "price": 120, "productName": "Espresso machine" }
  ],
  "total": 159.80,
  "customerName": "Mauro",
  "email": "[email protected]",
  "shippingAddress": "Via Roma 1, Milano",
  "estimatedDelivery": "2026-11-03"
}

That’s the spreadsheet in our mail merge.

Hosting Razor

Now we need the Word part: something that takes a template and fills it with the composed data. Email providers like SendGrid offer template engines, and in many cases they’re good enough. However, if we want to own templates, version them, and render them in our own process, we can host a template engine ourselves.

Razor is a natural choice in .NET: developers know it, and it’s expressive enough to handle lists, conditions, and formatting. Razor isn’t tied to ASP.NET MVC. Libraries like RazorLight compile and render Razor templates from strings at runtime, which is precisely what we need when users design templates and store them somewhere, rather than developers shipping them with the code.

Marketing reacts to the OrderPlaced event published by Sales. The message handler loads the template, asks the composition gateway for the model, renders the email, and sends it:

class OrderPlacedHandler(
    IEmailTemplates templates,
    IHttpClientFactory httpClientFactory,
    IRazorLightEngine razor,
    IEmailSender emailSender) : IHandleMessages<OrderPlaced>
{
    public async Task Handle(OrderPlaced message, IMessageHandlerContext context)
    {
        var template = await templates.Get("order-confirmation");

        var client = httpClientFactory.CreateClient("composition-gateway");
        var json = await client.GetStringAsync(
            $"/emails/order-confirmation/{message.OrderId}",
            context.CancellationToken);
        var model = JsonSerializer.Deserialize<OrderConfirmationEmailModel>(json, JsonSerializerOptions.Web);

        string body = await razor.CompileRenderStringAsync(
            $"{template.Name}-v{template.Version}",
            template.Content,
            model);

        await emailSender.Send(model.Email, template.Subject, body);
    }
}

IEmailTemplates and IEmailSender stand for wherever marketing stores templates and whatever it uses to send emails, an SMTP server or a provider API.

The handler deserializes the composed response into a strongly typed model, defined by Marketing:

public record OrderConfirmationEmailModel(
    string CustomerName,
    string Email,
    IReadOnlyList<OrderConfirmationEmailItem> Items,
    decimal Total,
    string ShippingAddress,
    DateOnly EstimatedDelivery);

public record OrderConfirmationEmailItem(string ProductName, int Quantity, decimal Price);

Isn’t that the shared model problem all over again? I discussed in “ServiceComposer: how do strongly-typed models affect composition and decomposition?” how a strongly typed response model shared by all services becomes a shared kernel nobody owns. That’s not the case here. In this scenario, Marketing acts like a branding service: it owns the template and, by definition, also the data model required to fill it. The model is the template’s contract with the outside world. Sales, Shipping, and Customers don’t reference it; they contribute their data to the composed route, and only Marketing turns the response into its model. It’s the same idea as each service defining its own request model during decomposition: the consumer owns the shape it needs. The model also ignores what the template doesn’t need, such as product identifiers.

Could OrderConfirmationEmailModel be the strongly typed composition model itself, created by a view model factory and filled by each handler? Technically, yes, but it would undo the point. Sales, Shipping, and Customers would reference a Marketing type, or Marketing’s model would need to implement interfaces owned by each of them, turning Marketing into the composition host for this route. Lists make it worse: Sales would need to create items of a type Marketing defines. And it wouldn’t gain much: the gateway and the message handler are separate processes, so the handler would still deserialize the response. That’s why the composition stays dynamic, and Marketing turns the result into its own model.

The template is what marketing users design:

@model Marketing.OrderConfirmationEmailModel
<p>Hi @Model.CustomerName, thanks for your order!</p>
<table>
@foreach (var item in Model.Items)
{
    <tr>
        <td>@item.Quantity x @item.ProductName</td>
        <td>@item.Price.ToString("C")</td>
    </tr>
}
</table>
<p>Total: @Model.Total.ToString("C")</p>
<p>
    We'll ship it to @Model.ShippingAddress.
    It should arrive by @Model.EstimatedDelivery.ToString("D").
</p>

The strongly typed model also pays off for template authors. Values have types, so the estimated delivery is a date that can be formatted, not a string to parse. And a typo, such as @Model.Total, fails when the template is compiled, not when a customer opens a broken email. Marketing can compile templates when users save them and reject the ones that don’t compile.

One more thing to keep in mind: RazorLight caches compiled templates by key. When users change a template, the key must change too. Otherwise, RazorLight keeps rendering the old compiled version. That’s why the key includes the template version.

In 2021, I mentioned that calling the composition gateway over HTTP from a message handler smells of temporal coupling. It does, but it’s contained: if the gateway or one of the services is unavailable, the handler fails, and the incoming message is retried later. Retries come with a different problem, though: sending an email isn’t idempotent. If the handler fails after sending, the customer receives the email twice, or seven times, as I described in “Seven emails later”.

Templates are code

There’s a catch I don’t want to hide. A Razor template isn’t a document with placeholders: it’s compiled into C# and executed. Anyone who can edit a template can run code inside the marketing process, with the same permissions the process has. A template can read files, call external services, or loop forever.

If template designers are developers or trusted users, and templates go through some form of review before going live, that’s acceptable. If templates are designed by people outside the development team, or worse, by customers, Razor is the wrong tool. Template languages such as Liquid, through libraries like Fluid, or Scriban, are designed to be sandboxed: templates can only access the model they’re given.

The good news is that the choice doesn’t affect the architecture. The composed model, the dedicated route, and the message handler stay the same. Only the rendering step changes.

Conclusion

An email is a frontend, and like any frontend, it needs data owned by many services. Mail merge is a useful mental model: marketing owns the document and the model it needs, ViewModel Composition provides the data, and a template engine hosted by marketing merges the two. Razor is a natural fit for .NET teams, as long as we remember that a Razor template is code and treat it accordingly.


Photo by Stephen Phillips - Hostreviews.co.uk on Unsplash