Best Design Practice for replacing tags in email

I have multiple tables and multiple email templates which will need tags replacing. The data for the tags depends on the recipient id as the data will need to be fetched from different tables.

I have a dictionary and register a different email sender depending on the enum like so

public class EmailService
{
    private readonly Dictionary<SluRecipients, IEmailSender?> _emailSenders;
    private readonly DbContext _context;

    public EmailService(ReghoundContext context, IServiceProvider serviceProvider)
    {
        _context = context;
        _emailSenders = new Dictionary<SluRecipients, IEmailSender?>
        {
            { SluRecipients.Alert, serviceProvider.GetService<IAlertEmailSender>() },
            { SluRecipients.Company, serviceProvider.GetService<ICompanyEmailSender>() }
        };
    }

    public async Task SendEmailAsync(int emailTemplateId, SendEmailRequest request)
    {
        var emailTemplate = await _context.TblEmailTemplates.FirstOrDefaultAsync(t => t.EmtpId == emailTemplateId);

        if (!_emailSenders.TryGetValue((SluRecipients)emailTemplate.ErecId, out var emailSender) || emailSender == null)
        {
            throw new InvalidOperationException($"No email sender found for key: {emailTemplate.ErecId}");
        }

        await emailSender.SendEmailAsync(emailTemplate, request);
    }

}

Now for email senders I have a base class which sends email alongside override methods to generate different content. e.g

public abstract class EmailSenderBase(DbContext context, IEmailRepository emailRepository, IEmailContentProcessor contentProcessor) : IEmailSender
{
    protected readonly DbContext _context = context;
    protected readonly IEmailRepository _emailRepository = emailRepository;
    protected readonly IEmailContentProcessor _contentProcessor = contentProcessor;

    public async Task SendEmailAsync(TblEmailTemplate template, SendEmailRequest request)
    {
        var data = await GetDataAsync(request) ?? throw new InvalidOperationException("Could not find data");
        var subject = await ProcessContentAsync(template.EmtpSubject, data, true);
        var body = await ProcessContentAsync(template.EmtpBody, data, false);

        var recipientEmails = GetRecipientEmails(template.ErecId, data);
        await _emailRepository.SendEmailAsync(recipientEmails, subject, body, body);
    }

    protected abstract Task<object> GetDataAsync(SendEmailRequest request);
    protected abstract Task<string> ProcessContentAsync(string content, object data, bool isSubject);
    protected abstract string[] GetRecipientEmails(int recipientType, object data);
}

Then in a concrete email sender I can override the methods like so

public class AlertEmailSender(DbContext context, IEmailRepository emailRepository, IAlertEmailContentProcessor contentProcessor) : 
    EmailSenderBase(context, emailRepository, contentProcessor), IAlertEmailSender
{
    protected override async Task<object> GetDataAsync(SendEmailRequest request)
    {
        if (request.AlertId == null)
            throw new ArgumentNullException(nameof(request));

        return await _context.TblAlerts.FindAsync(request.AlertId) ?? throw new InvalidOperationException("Alert not found");
    }

    protected override string[] GetRecipientEmails(int recipientType, object data)
    {
        var alert = (TblAlert)data;
        return alert.AlertEmails.Split(",");
    }

    protected override async Task<string> ProcessContentAsync(string content, object data, bool isSubject)
    {
        return await contentProcessor.ProcessContentAsync(content, data, isSubject);
    }
}

Then in each email sender I will replace content like so (this is done inside a IContentProcessor and will have a different one for each email sender.)

 public async Task<string> ProcessContentAsync(string content, object data, bool isSubject)
 {
     var alert = (TblAlert)data;
     var tags = await context.SluEmailTags.Where(t => isSubject ? t.EmtgIsSubject : t.EmtgIsBody).ToListAsync();
     var regex = EmailTagRegex();
     var matches = regex.Matches(content);

     foreach (Match match in matches.Cast<Match>())
     {
         var tagName = match.Groups[1].Value;
         var tag = tags.FirstOrDefault(t => t.EmtgName == tagName);
         if (tag == null) continue;

         if (tagName == SluEmailTags.AlertRecipient.EmtgName)
         {
             content = content.Replace(match.Value, alert.AlertRecipient);
         }
     }

     return content;
 }

This is the basic functionallity I am going with but this solution seems flawed in many ways. Has anyone done something similar and have a better approach than this?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật