Add user registration emails + email infrastructure (#150)

* Add email functionality

* Add email template project and rendering service

* Update email template dir structure

* Add email header and footer components for user registration template

* update example env

* Refactor email templates namespace and components

* Format email dir
This commit is contained in:
Aaron Po
2026-02-13 21:46:19 -05:00
committed by GitHub
parent 82f0d26200
commit 6b66f5680f
21 changed files with 615 additions and 30 deletions

View File

@@ -0,0 +1,58 @@
using Infrastructure.Email.Templates.Mail;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Logging;
namespace Infrastructure.Email.Templates.Rendering;
/// <summary>
/// Service for rendering Razor email templates to HTML using HtmlRenderer.
/// </summary>
public class EmailTemplateProvider(
IServiceProvider serviceProvider,
ILoggerFactory loggerFactory
) : IEmailTemplateProvider
{
/// <summary>
/// Renders the UserRegisteredEmail template with the specified parameters.
/// </summary>
public async Task<string> RenderUserRegisteredEmailAsync(
string username,
string confirmationLink
)
{
var parameters = new Dictionary<string, object?>
{
{ nameof(UserRegistration.Username), username },
{ nameof(UserRegistration.ConfirmationLink), confirmationLink },
};
return await RenderComponentAsync<UserRegistration>(parameters);
}
/// <summary>
/// Generic method to render any Razor component to HTML.
/// </summary>
private async Task<string> RenderComponentAsync<TComponent>(
Dictionary<string, object?> parameters
)
where TComponent : IComponent
{
await using var htmlRenderer = new HtmlRenderer(
serviceProvider,
loggerFactory
);
var html = await htmlRenderer.Dispatcher.InvokeAsync(async () =>
{
var parameterView = ParameterView.FromDictionary(parameters);
var output = await htmlRenderer.RenderComponentAsync<TComponent>(
parameterView
);
return output.ToHtmlString();
});
return html;
}
}