diff --git a/src/Sitko.Core.Email/FluentMailSender.cs b/src/Sitko.Core.Email/FluentMailSender.cs index 574deac36..2e2a969c1 100644 --- a/src/Sitko.Core.Email/FluentMailSender.cs +++ b/src/Sitko.Core.Email/FluentMailSender.cs @@ -2,6 +2,8 @@ using FluentEmail.Core.Models; using Hangfire; using HtmlAgilityPack; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Web; using Microsoft.Extensions.Logging; using Razor.Templating.Core; using Sitko.Core.App.Results; @@ -13,12 +15,17 @@ public class FluentMailSender : IMailSender where TOptions : EmailModu private readonly IBackgroundJobClient? backgroundJobClient; private readonly IFluentEmailFactory emailFactory; private readonly ILogger> logger; + private readonly IServiceProvider serviceProvider; + private readonly ILoggerFactory loggerFactory; public FluentMailSender(IFluentEmailFactory emailFactory, - ILogger> logger, IBackgroundJobClient? backgroundJobClient = null) + ILogger> logger, IServiceProvider serviceProvider, + ILoggerFactory loggerFactory, IBackgroundJobClient? backgroundJobClient = null) { this.emailFactory = emailFactory; this.logger = logger; + this.serviceProvider = serviceProvider; + this.loggerFactory = loggerFactory; this.backgroundJobClient = backgroundJobClient; } @@ -34,6 +41,19 @@ public async Task SendHtmlMailAsync(MailEntry mailEntry, strin return await SendMailAsync(mailEntry, html); } + public async Task SendHtmlMailAsync(MailEntry mailEntry, Dictionary data) where T : IComponent + { + await using var htmlRenderer = new HtmlRenderer(serviceProvider, loggerFactory); + var html = await htmlRenderer.Dispatcher.InvokeAsync(async () => + { + var parameters = ParameterView.FromDictionary(data); + var output = await htmlRenderer.RenderComponentAsync(parameters); + + return output.ToHtmlString(); + }); + return await SendMailAsync(mailEntry, html); + } + public async Task SendMailAsync(MailEntry mailEntry, string body) { var htmlDoc = new HtmlDocument(); diff --git a/src/Sitko.Core.Email/IMailSender.cs b/src/Sitko.Core.Email/IMailSender.cs index c63c6b577..c7c224458 100644 --- a/src/Sitko.Core.Email/IMailSender.cs +++ b/src/Sitko.Core.Email/IMailSender.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Components; using Sitko.Core.App.Results; namespace Sitko.Core.Email; @@ -6,6 +7,7 @@ public interface IMailSender { Task SendHtmlMailAsync(MailEntry mailEntry, string templatePath); Task SendHtmlMailAsync(MailEntry mailEntry, string templatePath); + Task SendHtmlMailAsync(MailEntry mailEntry, Dictionary data) where T : IComponent; Task SendMailAsync(MailEntry mailEntry, string body); void SendInBackground(MailEntry mailEntry, string templatePath); } diff --git a/tests/Sitko.Core.Email.Tests/BasicTests.cs b/tests/Sitko.Core.Email.Tests/BasicTests.cs index 947010425..697ab59a6 100644 --- a/tests/Sitko.Core.Email.Tests/BasicTests.cs +++ b/tests/Sitko.Core.Email.Tests/BasicTests.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.Configuration; +using Sitko.Core.Email.Tests.Data; using Sitko.Core.Xunit; using Xunit; using Xunit.Abstractions; @@ -31,8 +31,9 @@ public async Task Send() new MailEntryAttachment(imgName, "image/jpg", imgFile) } }; + var data = new Dictionary { { "Model", new TestModel() } }; - var result = await mailer.SendMailAsync(entry, "Test email"); + var result = await mailer.SendHtmlMailAsync(entry, data); Assert.True(result.IsSuccess, result.ErrorMessage); } } diff --git a/tests/Sitko.Core.Email.Tests/Data/TestComponent.razor b/tests/Sitko.Core.Email.Tests/Data/TestComponent.razor new file mode 100644 index 000000000..8765317cf --- /dev/null +++ b/tests/Sitko.Core.Email.Tests/Data/TestComponent.razor @@ -0,0 +1,7 @@ +@inherits ComponentBase + +

@Model.Text

+ +@code { + [Parameter] public TestModel Model { get; set; } = null!; +} diff --git a/tests/Sitko.Core.Email.Tests/Data/TestModel.cs b/tests/Sitko.Core.Email.Tests/Data/TestModel.cs new file mode 100644 index 000000000..edcefdaf6 --- /dev/null +++ b/tests/Sitko.Core.Email.Tests/Data/TestModel.cs @@ -0,0 +1,6 @@ +namespace Sitko.Core.Email.Tests.Data; + +public class TestModel +{ + public string Text { get; set; } = "Test email"; +} diff --git a/tests/Sitko.Core.Email.Tests/Sitko.Core.Email.Tests.csproj b/tests/Sitko.Core.Email.Tests/Sitko.Core.Email.Tests.csproj index fbe7f9182..86974fe7e 100644 --- a/tests/Sitko.Core.Email.Tests/Sitko.Core.Email.Tests.csproj +++ b/tests/Sitko.Core.Email.Tests/Sitko.Core.Email.Tests.csproj @@ -1,4 +1,4 @@ - +