Add CC and BCC to emails

This commit is contained in:
Nicolas Dorier
2025-11-08 22:42:45 +09:00
parent d7fcd55707
commit dcf60e20b9
16 changed files with 442 additions and 177 deletions

View File

@@ -2,7 +2,9 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Mail;
using BTCPayServer.Data;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Plugins.Emails.Views;
@@ -25,6 +27,8 @@ public class StoreEmailRuleViewModel
Condition = data.Condition ?? "";
Body = data.Body;
To = string.Join(",", data.To);
CC = string.Join(",", data.CC);
BCC = string.Join(",", data.BCC);
}
else
{
@@ -46,6 +50,8 @@ public class StoreEmailRuleViewModel
public EmailRuleData Data { get; set; }
public EmailRuleData.BTCPayAdditionalData AdditionalData { get; set; }
public string To { get; set; }
public string CC { get; set; }
public string BCC { get; set; }
public List<EmailTriggerViewModel> Triggers { get; set; }
public string RedirectUrl { get; set; }
@@ -54,8 +60,29 @@ public class StoreEmailRuleViewModel
public string OfferingId { get; set; }
public string StoreId { get; set; }
public string[] ToAsArray()
=> (To ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Trim())
.ToArray();
public string[] AsArray(string values)
{
// This replace the placeholders with random email addresses
// We can't just split input by comma, because display names of people can contain commas.
// "John, Jr. Smith" <jon@example.com>,{User.Email},"Nicolas D." <nico@example.com>
values ??= "";
// We replace the placeholders with dummy email addresses
var template = new TextTemplate(values);
var dummy = $"{Random.Shared.Next()}@example.com";
template.NotFoundReplacement = o => $"\"{o}\" <{dummy}>";
values = template.Render(new JObject());
if (string.IsNullOrWhiteSpace(values))
return Array.Empty<string>();
// We use MailAddressCollection to parse the addresses
MailAddressCollection mailCollection = new();
mailCollection.Add(values);
foreach (var mail in mailCollection)
{
// Let it throw if the address is invalid
MailboxAddressValidator.Parse(mail.ToString());
}
// We replace the dummies with the former placeholders
return mailCollection.Select(a => a.Address == dummy ? $"{{{a.DisplayName}}}" : a.ToString()).ToArray();
}
}