public async Task SendEmailsAsync() { log.Info($"Loading config from {configFilename} (see example in {configFilename}.example)"); OneTimeEmailSenderConfig config; using (var stream = new StreamReader(configFilename)) config = (OneTimeEmailSenderConfig) new XmlSerializer(typeof(OneTimeEmailSenderConfig)).Deserialize(stream); log.Info($"Loaded config from {configFilename}:"); log.Info(config.XmlSerialize()); /* Get text from html by stripping HTML tags if text is not defined */ if (string.IsNullOrEmpty(config.Text)) { config.Text = config.Html.StripHtmlTags(); } config.Text = config.Text.RemoveCommonNesting().Trim(); foreach (var email in config.Emails) { log.Info($"Send email to {email}"); var button = config.Button == null ? null : new EmailButton(config.Button.Link, config.Button.Text); await emailSender.SendEmailAsync(email, config.Subject, config.Text, config.Html, button).ConfigureAwait(false); } }
public async Task SendEmailsAsync() { log.Info($"Loading config from {configFilename} (see example in {configFilename}.example)"); OneTimeEmailSenderConfig config; using (var stream = new StreamReader(configFilename)) config = (OneTimeEmailSenderConfig) new XmlSerializer(typeof(OneTimeEmailSenderConfig)).Deserialize(stream); log.Info($"Loaded config from {configFilename}:"); log.Info(config.XmlSerialize()); /* Get text from html by stripping HTML tags if text is not defined */ if (string.IsNullOrEmpty(config.Text)) { config.Text = config.Html.StripHtmlTags(); } config.Text = config.Text.RemoveCommonNesting().Trim(); var emails = config.Emails.ToImmutableHashSet(); var email2User = usersRepo.FindUsersByConfirmedEmails(emails).ToDictSafe(u => u.Email, u => u); foreach (var email in emails) { if (!email2User.TryGetValue(email, out var user)) { log.Warn($"User with confirmed email not found for {email}"); continue; } var mailTransport = notificationsRepo.FindUsersNotificationTransport <MailNotificationTransport>(user.Id); if (mailTransport == null) { log.Warn($"Mail transport not enabled for {email}"); continue; } var settings = notificationsRepo.GetNotificationTransportsSettings(config.CourseId, NotificationType.SystemMessage, new List <int> { mailTransport.Id }); const bool isEnabledByDefault = true; var mailSettings = settings[mailTransport.Id]; if (mailSettings == null && !isEnabledByDefault || mailSettings != null && !settings[mailTransport.Id].IsEnabled) { log.Warn($"SystemMessage for mail transport for {config.CourseId} not enabled for {email}"); continue; } log.Info($"Send email to {email}"); var button = config.Button == null ? null : new EmailButton(config.Button.Link, config.Button.Text); await emailSender.SendEmailAsync(email, config.Subject, config.Text, config.Html, button).ConfigureAwait(false); } }