public async Task <int> AddTemplateAsync(DirectEmailTemplate directEmailTemplate) { if (directEmailTemplate == null) { throw new ArgumentNullException(nameof(directEmailTemplate)); } var baseExists = await _emailBaseRepository .GetByIdAsync(directEmailTemplate.EmailBaseId); if (baseExists == null) { throw new GraException($"Could not find base email template id {directEmailTemplate.EmailBaseId}"); } return(await _directEmailTemplateRepository .AddSaveWithTextAsync(GetActiveUserId(), directEmailTemplate)); }
public async Task UpdateTemplateAsync(DirectEmailTemplate directEmailTemplate) { var updated = await _directEmailTemplateRepository .UpdateSaveWithTextAsync(GetActiveUserId(), directEmailTemplate); if (directEmailTemplate?.DirectEmailTemplateText?.LanguageId != null) { if (!string.IsNullOrEmpty(updated.SystemEmailId)) { await _cache.RemoveAsync(GetCacheKey(CacheKey.DirectEmailTemplateSystemId, updated.SystemEmailId, directEmailTemplate.DirectEmailTemplateText.LanguageId)); } await _cache.RemoveAsync(GetCacheKey(CacheKey.DirectEmailTemplateId, directEmailTemplate.Id, directEmailTemplate.DirectEmailTemplateText.LanguageId)); } }
public async Task <DirectEmailHistory> SendDirectAsync(DirectEmailDetails directEmailDetails) { if (directEmailDetails == null) { throw new ArgumentNullException(nameof(directEmailDetails)); } string toAddress; string toName; int languageId; Site site; if (directEmailDetails.ToUserId.HasValue) { var user = await _userRepository.GetByIdAsync(directEmailDetails.ToUserId ?? directEmailDetails.SendingUserId); if (string.IsNullOrEmpty(user.Email)) { _logger.LogError("Unable to send email to user id {UserId}: no email address configured.", directEmailDetails.ToUserId); throw new GraException($"User id {directEmailDetails.ToUserId} does not have an email address configured."); } site = await _siteLookupService.GetByIdAsync(user.SiteId); toAddress = user.Email; toName = user.FullName; languageId = directEmailDetails.LanguageId ?? (string.IsNullOrEmpty(user.Culture) ? await _languageService.GetDefaultLanguageIdAsync() : await _languageService.GetLanguageIdAsync(user.Culture)); } else { var user = await _userRepository.GetByIdAsync(directEmailDetails.SendingUserId); site = await _siteLookupService.GetByIdAsync(user.SiteId); toAddress = directEmailDetails.ToAddress; toName = directEmailDetails.ToName; languageId = directEmailDetails.LanguageId ?? await _languageService.GetLanguageIdAsync(CultureInfo.CurrentCulture.Name); } if (!SiteCanSendMail(site)) { throw new GraException("Unable to send mail, please ensure from name, from email, and outgoing mail server are configured in Site Management -> Configuration."); } var history = new DirectEmailHistory { CreatedBy = directEmailDetails.SendingUserId, FromEmailAddress = site.FromEmailAddress, FromName = site.FromEmailName, IsBulk = directEmailDetails.IsBulk, LanguageId = languageId, OverrideToEmailAddress = string .IsNullOrWhiteSpace(_config[ConfigurationKey.EmailOverride]) ? null : _config[ConfigurationKey.EmailOverride], ToEmailAddress = toAddress, ToName = toName }; if (directEmailDetails.ToUserId.HasValue) { history.UserId = directEmailDetails.ToUserId.Value; } DirectEmailTemplate directEmailTemplate = await GetDirectEmailTemplateAsync(directEmailDetails.DirectEmailSystemId, directEmailDetails.DirectEmailTemplateId, history.LanguageId); if (directEmailTemplate == null || directEmailTemplate.DirectEmailTemplateText == null) { // not available in the requested language, use the default language history.LanguageId = await _languageService.GetDefaultLanguageIdAsync(); directEmailTemplate = await GetDirectEmailTemplateAsync(directEmailDetails.DirectEmailSystemId, directEmailDetails.DirectEmailTemplateId, history.LanguageId); } history.DirectEmailTemplateId = directEmailTemplate.Id; history.EmailBaseId = directEmailTemplate.EmailBaseId; var stubble = new StubbleBuilder().Build(); history.Subject = await stubble .RenderAsync(directEmailTemplate.DirectEmailTemplateText.Subject, directEmailDetails.Tags); history.BodyText = await stubble .RenderAsync(directEmailTemplate.DirectEmailTemplateText.BodyCommonMark, directEmailDetails.Tags); history.BodyHtml = CommonMark.CommonMarkConverter.Convert(history.BodyText); string preview = await stubble .RenderAsync(directEmailTemplate.DirectEmailTemplateText.Preview, directEmailDetails.Tags); string title = await stubble .RenderAsync(directEmailTemplate.DirectEmailTemplateText.Title, directEmailDetails.Tags); string footer = CommonMark.CommonMarkConverter.Convert(await stubble .RenderAsync(directEmailTemplate.DirectEmailTemplateText.Footer, directEmailDetails.Tags)); history = await InternalSendDirectAsync(site, history, new Dictionary <string, string> { { "Footer", footer }, { "Preview", preview }, { "Title", title }, { "BodyHtml", history.BodyHtml }, { "BodyText", history.BodyText } }); if (directEmailDetails.IsBulk && !directEmailDetails.IsTest) { if (directEmailDetails.ToUserId.HasValue) { history.BodyHtml = null; history.BodyText = null; await _directEmailHistoryRepository.AddSaveNoAuditAsync(history); } if (!directEmailTemplate.SentBulk) { await _directEmailTemplateRepository .UpdateSentBulkAsync(directEmailTemplate.Id); } } else { if (!directEmailDetails.IsTest) { await _directEmailHistoryRepository.AddSaveNoAuditAsync(history); } await IncrementSentCountAsync(directEmailTemplate.Id); } return(history); }