|
| 1 | +# PosInformatique.Foundations.Emailing.Templates.Razor |
| 2 | + |
| 3 | +[](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing.Templates.Razor/) |
| 4 | +[](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing.Templates.Razor/) |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +[PosInformatique.Foundations.Emailing.Templates.Razor](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing.Templates.Razor/) |
| 9 | +provides helpers to create `EmailTemplate<TModel>` instances using Razor components as text templates for the subject and HTML body. |
| 10 | + |
| 11 | +It is built on top of: |
| 12 | + |
| 13 | +- [PosInformatique.Foundations.Emailing](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing.Templates.Razor/) |
| 14 | +- [PosInformatique.Foundations.Text.Templates.Razor](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing.Templates.Razor/) |
| 15 | + |
| 16 | +This allows you to design your email content with Blazor-style Razor components, benefiting from layout reuse, strongly-typed models, and familiar Razor syntax. |
| 17 | + |
| 18 | +## Install |
| 19 | + |
| 20 | +You can install the package from [NuGet](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing.Templates.Razor/): |
| 21 | + |
| 22 | +```powershell |
| 23 | +dotnet add package PosInformatique.Foundations.Emailing.Templates.Razor |
| 24 | +``` |
| 25 | + |
| 26 | +You also need a Blazor-compatible environment for compiling/executing Razor components. |
| 27 | + |
| 28 | +## Features |
| 29 | + |
| 30 | +- `RazorEmailTemplate<TModel>.Create<TSubjectComponent, TBodyComponent>()` to build `EmailTemplate<TModel>` from Razor components. |
| 31 | +- Base classes for Razor components: |
| 32 | + - `RazorEmailTemplateSubject<TModel>` for email subject. |
| 33 | + - `RazorEmailTemplateBody<TModel>` for email HTML body. |
| 34 | +- Strongly-typed model support via the `Model` parameter. |
| 35 | +- Supports Razor layout features for the email body (reuse consistent layout across multiple templates). |
| 36 | + |
| 37 | +## Creating Razor components for subject and body |
| 38 | + |
| 39 | +### 1. Define the email model |
| 40 | + |
| 41 | +Example model used by the templates: |
| 42 | + |
| 43 | +```csharp |
| 44 | +using PosInformatique.Foundations.Emailing; |
| 45 | + |
| 46 | +public sealed class InvitationEmailTemplateModel : EmailModel |
| 47 | +{ |
| 48 | + public string FirstName { get; set; } = string.Empty; |
| 49 | + public string InvitationLink { get; set; } = string.Empty; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### 2. Subject component |
| 54 | + |
| 55 | +Create a Razor component for the subject that inherits from `RazorEmailTemplateSubject<TModel>` and uses the `Model` parameter. |
| 56 | + |
| 57 | +`InvitationEmailSubject.razor`: |
| 58 | + |
| 59 | +```razor |
| 60 | +@using PosInformatique.Foundations.Emailing |
| 61 | +@using PosInformatique.Foundations.Emailing.Templates.Razor |
| 62 | +@inherits RazorEmailTemplateSubject<InvitationEmailTemplateModel> |
| 63 | +
|
| 64 | +Invitation for @Model.FirstName |
| 65 | +``` |
| 66 | + |
| 67 | +This component: |
| 68 | + |
| 69 | +- Renders a single line of text. |
| 70 | +- Uses the strongly-typed `Model` to build the subject. |
| 71 | + |
| 72 | +### 3. Body component with layout |
| 73 | + |
| 74 | +You can define a layout component that centralizes common HTML structure (header, footer, styles, etc.), |
| 75 | +then reuse it across different email bodies. |
| 76 | + |
| 77 | +`EmailLayout.razor` (layout component): |
| 78 | + |
| 79 | +```razor |
| 80 | +@inherits LayoutComponentBase |
| 81 | +
|
| 82 | +<!DOCTYPE html> |
| 83 | +<html> |
| 84 | +<head> |
| 85 | + <meta charset="utf-8" /> |
| 86 | + <title>@Title</title> |
| 87 | + <style> |
| 88 | + body { |
| 89 | + font-family: Arial, sans-serif; |
| 90 | + font-size: 14px; |
| 91 | + } |
| 92 | +
|
| 93 | + .email-container { |
| 94 | + max-width: 600px; |
| 95 | + margin: 0 auto; |
| 96 | + } |
| 97 | +
|
| 98 | + .email-header { |
| 99 | + background-color: #1f2937; |
| 100 | + color: #ffffff; |
| 101 | + padding: 16px; |
| 102 | + font-size: 18px; |
| 103 | + font-weight: bold; |
| 104 | + } |
| 105 | +
|
| 106 | + .email-content { |
| 107 | + padding: 16px; |
| 108 | + } |
| 109 | +
|
| 110 | + .email-footer { |
| 111 | + padding: 16px; |
| 112 | + font-size: 12px; |
| 113 | + color: #6b7280; |
| 114 | + } |
| 115 | + </style> |
| 116 | +</head> |
| 117 | +<body> |
| 118 | + <div class="email-container"> |
| 119 | + <div class="email-header"> |
| 120 | + @Title |
| 121 | + </div> |
| 122 | +
|
| 123 | + <div class="email-content"> |
| 124 | + @Body |
| 125 | + </div> |
| 126 | +
|
| 127 | + <div class="email-footer"> |
| 128 | + This email was sent by MyApp. |
| 129 | + </div> |
| 130 | + </div> |
| 131 | +</body> |
| 132 | +</html> |
| 133 | +``` |
| 134 | + |
| 135 | +Now create the specific body component for your invitation email. |
| 136 | + |
| 137 | +`InvitationEmailBody.razor`: |
| 138 | + |
| 139 | +```razor |
| 140 | +@using PosInformatique.Foundations.Emailing |
| 141 | +@using PosInformatique.Foundations.Emailing.Templates.Razor |
| 142 | +@inherits RazorEmailTemplateBody<InvitationEmailTemplateModel> |
| 143 | +@layout EmailLayout |
| 144 | +
|
| 145 | +@{ |
| 146 | + Title = $"Invitation for {Model.FirstName}"; |
| 147 | +} |
| 148 | +
|
| 149 | +<p>Hello @Model.FirstName,</p> |
| 150 | +
|
| 151 | +<p> |
| 152 | + You have been invited to join our platform. |
| 153 | + Please click the link below to accept your invitation: |
| 154 | +</p> |
| 155 | +
|
| 156 | +<p> |
| 157 | + <a href="@Model.InvitationLink">@Model.InvitationLink</a> |
| 158 | +</p> |
| 159 | +
|
| 160 | +<p> |
| 161 | + If you did not expect this email, you can safely ignore it. |
| 162 | +</p> |
| 163 | +``` |
| 164 | + |
| 165 | +Key points: |
| 166 | + |
| 167 | +- The body component inherits from `RazorEmailTemplateBody<InvitationEmailTemplateModel>`. |
| 168 | +- It uses `@layout EmailLayout` to reuse the common HTML structure. |
| 169 | +- It uses the `Model` property to inject data into the HTML. |
| 170 | + |
| 171 | +## Creating an EmailTemplate from Razor components |
| 172 | + |
| 173 | +Use the static helper `RazorEmailTemplate<TModel>.Create<TSubjectComponent, TBodyComponent>()` to build an `EmailTemplate<TModel>` instance. |
| 174 | + |
| 175 | +```csharp |
| 176 | +using PosInformatique.Foundations.Emailing; |
| 177 | +using PosInformatique.Foundations.Emailing.Templates.Razor; |
| 178 | + |
| 179 | +var invitationTemplate = RazorEmailTemplate<InvitationEmailTemplateModel>.Create< |
| 180 | + InvitationEmailSubject, |
| 181 | + InvitationEmailBody>(); |
| 182 | +``` |
| 183 | + |
| 184 | +You can then register this template in `EmailingOptions`: |
| 185 | + |
| 186 | +```csharp |
| 187 | +options.RegisterTemplate(EmailTemplateIdentifiers.Invitation, invitationTemplate); |
| 188 | +``` |
| 189 | + |
| 190 | +After that, the rest of the flow is the same as with any other `EmailTemplate<TModel>`: |
| 191 | + |
| 192 | +- Use `IEmailManager.Create(EmailTemplateIdentifiers.Invitation)` to create the email. |
| 193 | +- Add recipients and models. |
| 194 | +- Call `SendAsync(...)` to send. |
| 195 | + |
| 196 | +## Links |
| 197 | + |
| 198 | +- [NuGet package: Emailing (core library)](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing/) |
| 199 | +- [NuGet package: Emailing.Templates.Razor](https://www.nuget.org/packages/PosInformatique.Foundations.Emailing.Templates.Razor/) |
| 200 | +- [NuGet package: Text.Templating.Razor](https://www.nuget.org/packages/PosInformatique.Foundations.Text.Templating.Razor/) |
| 201 | +- [Source code](https://github.com/PosInformatique/PosInformatique.Foundations) |
0 commit comments