using Infrastructure.Email; using Infrastructure.Email.Templates.Rendering; namespace Features.Emails.Services; /// /// Default implementation of that renders email templates and dispatches /// them via an . /// /// Provider used to deliver the rendered emails. /// Provider used to render HTML email bodies from templates. public class EmailDispatcher( IEmailProvider emailProvider, IEmailTemplateProvider emailTemplateProvider ) : IEmailDispatcher { /// /// The base URL of the website, used to build confirmation links. Read from the /// WEBSITE_BASE_URL environment variable. /// /// /// Thrown at type initialization time when the WEBSITE_BASE_URL environment variable is not set. /// private static readonly string WebsiteBaseUrl = Environment.GetEnvironmentVariable("WEBSITE_BASE_URL") ?? throw new InvalidOperationException("WEBSITE_BASE_URL environment variable is not set"); /// /// Builds a confirmation link from the given token, renders the registration welcome email template, /// and sends it to the newly created user. /// public async Task SendRegistrationEmailAsync(string firstName, string email, string confirmationToken) { string confirmationLink = $"{WebsiteBaseUrl}/users/confirm?token={confirmationToken}"; string emailHtml = await emailTemplateProvider.RenderUserRegisteredEmailAsync( firstName, confirmationLink ); await emailProvider.SendAsync( email, "Welcome to The Biergarten App!", emailHtml, true ); } /// /// Builds a confirmation link from the given token, renders the resend-confirmation email template, /// and sends it to the user. /// public async Task SendResendConfirmationEmailAsync(string firstName, string email, string confirmationToken) { string confirmationLink = $"{WebsiteBaseUrl}/users/confirm?token={confirmationToken}"; string emailHtml = await emailTemplateProvider.RenderResendConfirmationEmailAsync( firstName, confirmationLink ); await emailProvider.SendAsync( email, "Confirm Your Email - The Biergarten App", emailHtml, true ); } }