Added html partial for articles in email

This commit is contained in:
Mia Rose Winter 2024-02-14 12:51:03 +01:00
parent cf51a3b4ab
commit 3692853087
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 21 additions and 10 deletions

View file

@ -77,6 +77,15 @@ public enum Constants {
FileSystem.GetEmailTemplate("default", DefaultTemplates["default"]); FileSystem.GetEmailTemplate("default", DefaultTemplates["default"]);
FileSystem.GetEmailTemplate("newsletter", DefaultTemplates["newsletter"]); FileSystem.GetEmailTemplate("newsletter", DefaultTemplates["newsletter"]);
FileSystem.GetEmailTemplate("welcome", DefaultTemplates["welcome"]); FileSystem.GetEmailTemplate("welcome", DefaultTemplates["welcome"]);
FileSystem.GetPartialTemplateAsync("email-article",
"""
<div style="padding: 10px; background: #9f9f9f; color: #fff; margin-bottom: 10px; border-radius: 2px">
<h3 style="margin-top: 0;">{0}</h3>
<small>{1}</small>
<p>{2}...</p>
<a href="{3}">Link</a>
</div>
""");
} }
public string ApplyTokens(string template, Func<string, string?> replacer) { public string ApplyTokens(string template, Func<string, string?> replacer) {

View file

@ -11,10 +11,11 @@
namespace Wave.Services; namespace Wave.Services;
public class SmtpEmailSender(ILogger<SmtpEmailSender> logger, IOptions<SmtpConfiguration> config, IOptions<Customization> customizations, EmailTemplateService templateService) : IEmailSender<ApplicationUser>, IAdvancedEmailSender { public class SmtpEmailSender(ILogger<SmtpEmailSender> logger, IOptions<SmtpConfiguration> config, IOptions<Customization> customizations, EmailTemplateService templateService, FileSystemService fileSystemService) : IEmailSender<ApplicationUser>, IAdvancedEmailSender {
private ILogger<SmtpEmailSender> Logger { get; } = logger; private ILogger<SmtpEmailSender> Logger { get; } = logger;
private SmtpConfiguration Configuration { get; } = config.Value; private SmtpConfiguration Configuration { get; } = config.Value;
private Customization Customizations { get; } = customizations.Value; private Customization Customizations { get; } = customizations.Value;
private FileSystemService FileSystemService { get; } = fileSystemService;
private EmailTemplateService TemplateService { get; } = templateService; private EmailTemplateService TemplateService { get; } = templateService;
public Task SendConfirmationLinkAsync(ApplicationUser user, string email, string confirmationLink) => public Task SendConfirmationLinkAsync(ApplicationUser user, string email, string confirmationLink) =>
@ -106,19 +107,20 @@ public Task SendEmailAsync(string email, string? name, string subject, string ht
.CreateConfirmTokensAsync(subscriber.Id, "unsubscribe-welcome", TimeSpan.FromDays(30)); .CreateConfirmTokensAsync(subscriber.Id, "unsubscribe-welcome", TimeSpan.FromDays(30));
var host = new Uri(string.IsNullOrWhiteSpace(Customizations.AppUrl) ? "" : Customizations.AppUrl); // TODO get link var host = new Uri(string.IsNullOrWhiteSpace(Customizations.AppUrl) ? "" : Customizations.AppUrl); // TODO get link
string articlePartial = (await FileSystemService.GetPartialTemplateAsync("email-article", """
<div style="padding: 10px; background: #9f9f9f; color: #fff; margin-bottom: 10px; border-radius: 2px">
<h3 style="margin-top: 0;">{0}</h3>
<small>{1}</small>
<p>{2}...</p>
<a href="{3}">Link</a>
</div>
"""))!;
var articlesHtml = new StringBuilder(""); var articlesHtml = new StringBuilder("");
foreach (var n in articles) { foreach (var n in articles) {
string articleLink = ArticleUtilities.GenerateArticleLink(n.Article, new Uri(Customizations.AppUrl, UriKind.Absolute)); string articleLink = ArticleUtilities.GenerateArticleLink(n.Article, new Uri(Customizations.AppUrl, UriKind.Absolute));
articlesHtml.AppendFormat( articlesHtml.AppendFormat(
""" articlePartial,
<div style="padding: 10px; background: #333; color: #fff; margin-bottom: 10px"> n.Article.Title, n.Article.Author.Name, n.Article.Body[..Math.Min(250, n.Article.Body.Length)], articleLink);
<h3>{0}</h3>
<small>{1}</small>
<p>{2}</p>
<a href="{3}">Link</a>
</div>
""",
n.Article.Title, n.Article.Author.Name, n.Article.Body[..Math.Min(100, n.Article.Body.Length)], articleLink);
} }
string logo = !string.IsNullOrWhiteSpace(Customizations.LogoLink) string logo = !string.IsNullOrWhiteSpace(Customizations.LogoLink)