/// <summary> /// Applies the placeholders to the body and the subject properties /// </summary> /// <param name="mailTemplate"></param> /// <param name="logger"></param> /// <returns></returns> public async Task <Data.MailTemplate> ApplyPlaceholdersAsync(Data.MailTemplate mailTemplate, System.IO.TextWriter logger = null) { var placeholderGroupReplacements = mailTemplate.PlaceholderGroupList.PlaceholderGroupDictionary.Values.ToList(); var placeholderReplacements = mailTemplate.PlaceholderList.PlaceholderDictionary.Values.ToList(); var optionalSectionsReplacements = mailTemplate.OptionalSections; return(await _placeholderLogic.ApplyPlaceholdersAsync(mailTemplate, placeholderGroupReplacements, placeholderReplacements, optionalSectionsReplacements, logger)); }
/// <summary> /// Send the current mail template with the provided values to the handler /// </summary> /// <param name="id"></param> public async Task SendTestMailAsync(int id) { if (_sendPreviewEmailEventHandler == null) { wim.CurrentVisitor.Data.Apply("wim.note", $"No implementation for {nameof(ISendPreviewEmailEventHandler)} supplied, cannot send test mail"); await wim.CurrentVisitor.SaveAsync(); return; } var mailTemplate = await _mailTemplateRepository.FetchSingleAsync(id); var placeholderSubject = CreatePlaceholderObject(mailTemplate.Subject); var placeholderBody = CreatePlaceholderObject(mailTemplate.Body); var placeholderGroupSubject = CreatePlaceholderGroupsObject(mailTemplate.Subject); var placeholderGroupBody = CreatePlaceholderGroupsObject(mailTemplate.Body); var sectionBody = CreateSectionObject(mailTemplate.Body); mailTemplate = await _placeholderLogic.ApplyPlaceholdersAsync(mailTemplate, placeholderGroupBody, placeholderBody, sectionBody); var body = mailTemplate.Body; var subject = mailTemplate.Subject; if (string.IsNullOrWhiteSpace(subject)) { // not possible to send an e-mail without a subject wim.CurrentVisitor.Data.Apply("wim.note", "A subject is mandatory, please supply a subject before sending a test e-mail. E-mail wasn't sent."); await wim.CurrentVisitor.SaveAsync(); return; } var e = new SendPreviewEmailEventArgs { EmailFrom = EmailFrom, EmailTo = EmailTo, MailTemplate = mailTemplate, EmailFromName = mailTemplate.DefaultSenderName }; await _sendPreviewEmailEventHandler.SendPreviewEmailAsync(e); if (e.IsSuccess) { // send successfull wim.CurrentVisitor.Data.Apply("wim.note", "E-mail has been sent successfully"); await wim.CurrentVisitor.SaveAsync(); } else { wim.CurrentVisitor.Data.Apply("wim.note", $"E-mail wasn't sent properly, see '{nameof(SendTestMail_List)}' notifications for more details"); await Mediakiwi.Data.Notification.InsertOneAsync(nameof(SendTestMail_List), e.ErrorMessage); await wim.CurrentVisitor.SaveAsync(); } }