using DysonNetwork.Shared.Proto; using DysonNetwork.Shared.Templating; using Microsoft.AspNetCore.Components; namespace DysonNetwork.Pass.Mailer; public class EmailService( RingService.RingServiceClient pusher, RazorViewRenderer viewRenderer, ITemplateService templateService, ILogger logger ) { public async Task SendEmailAsync( string? recipientName, string recipientEmail, string subject, string htmlBody ) { await pusher.SendEmailAsync( new SendEmailRequest() { Email = new EmailMessage() { ToName = recipientName, ToAddress = recipientEmail, Subject = subject, Body = htmlBody } } ); } public async Task SendTemplatedEmailAsync(string? recipientName, string recipientEmail, string subject, TModel model) where TComponent : IComponent { try { var htmlBody = await viewRenderer.RenderComponentToStringAsync(model); await SendEmailAsync(recipientName, recipientEmail, subject, htmlBody); } catch (Exception err) { logger.LogError(err, "Failed to render email template..."); throw; } } /// /// Sends an email using a RazorLight template with locale support. /// /// The model type for the template. /// The recipient's display name. /// The recipient's email address. /// The email subject. /// The template name (e.g., "welcome", "factor-code"). /// The model data for the template. /// Optional locale override (defaults to CurrentUICulture). public async Task SendRazorTemplateEmailAsync(string? recipientName, string recipientEmail, string subject, string templateName, TModel model, string? locale = null) { try { var htmlBody = await templateService.RenderAsync(templateName, model, locale); await SendEmailAsync(recipientName, recipientEmail, subject, htmlBody); } catch (Exception err) { logger.LogError(err, "Failed to render RazorLight email template {TemplateName} for locale {Locale}", templateName, locale ?? "default"); throw; } } }