From 8f4518fef68c5326c7c99f71aac5ade5cab0160e Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Mon, 9 Sep 2024 17:05:59 +0200 Subject: [PATCH] Added email test function to settings page --- Wave/Components/Pages/Settings.razor | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/Wave/Components/Pages/Settings.razor b/Wave/Components/Pages/Settings.razor index 8b4cdfc..6b5ff55 100644 --- a/Wave/Components/Pages/Settings.razor +++ b/Wave/Components/Pages/Settings.razor @@ -1,8 +1,20 @@ @page "/settings" +@using Microsoft.AspNetCore.Identity +@using Microsoft.Extensions.Options +@using Wave.Data +@using Wave.Services +@using Wave.Utilities +@rendermode InteractiveServer @attribute [Authorize(Roles = "Admin")] @inject IStringLocalizer Localizer +@inject IOptions Features +@inject IServiceProvider ServiceProvider +@inject UserManager UserManager +@inject ILogger Logger +@inject IMessageDisplay Message +@inject NavigationManager Navigation @(Localizer["Title"] + TitlePostfix) @@ -12,6 +24,26 @@

@Localizer["Wave_Version_Label"] @Version

+ @if (Features.Value.EmailSubscriptions) { + +
+
+ Email +
+
+ + + + + + + +
+
+
+ } @code { @@ -19,5 +51,44 @@ private string TitlePostfix { get; set; } = default!; [CascadingParameter(Name = "Version")] private string Version { get; set; } = string.Empty; + [CascadingParameter] + private Task AuthenticationState { get; set; } = default!; + private string Email { get; set; } = string.Empty; + private string EmailType { get; set; } = "default"; + private bool EmailBusy = false; + + private async Task TestEmail() { + if (EmailBusy) return; + try { + EmailBusy = true; + await using var client = ServiceProvider.GetRequiredService(); + await client.ConnectAsync(CancellationToken.None); + var factory = ServiceProvider.GetRequiredService(); + + const string title = "Test Email"; + const string body = "Hello from Wave"; + EmailSubscriber sub = new() {Email = Email, Language = "en-US"}; + string author = await factory.CreateAuthorCard( + (await UserManager.GetUserAsync((await AuthenticationState).User))!, + new Uri(Navigation.BaseUri, UriKind.Absolute)); + + var email = EmailType switch { + "welcome" => await factory.CreateWelcomeEmail(sub, [], title, title, $"

{body}

", body), + "newsletter" => await factory.CreateSubscribedEmail(sub, + new Uri(Navigation.BaseUri, UriKind.Absolute).AbsoluteUri, + title, title, $"

{body}

" + author, body), + var _ => await factory.CreateDefaultEmail(sub.Email, sub.Name, title, title, $"

{body}

", body) + }; + + await client.SendEmailAsync(email, CancellationToken.None); + + Message.ShowSuccess("Test Email send", "Test Email"); + } catch (Exception ex) { + Message.ShowError("Failed to send email: " + ex.Message, "Test Email"); + Logger.LogError(ex, "Failed to send email"); + } finally { + EmailBusy = false; + } + } }