public IEnumerable <Notification> NotifySubscribersViaEmail <T>(string notificationTypeAlias, string emailTemplateAlias, T model, long userId, long requestedBy, string source, string notes = null, int priority = 0, DateTime?notificationDate = null, bool useAlternateEmail = false) { NotificationType notificationType = _notificationTypeRepository.GetByAlias(notificationTypeAlias); if (!notificationType.IsQueuingEnabled) { return(null); } EmailTemplate emailTemplate = _emailTemplateRepository.GetByAlias(emailTemplateAlias); List <NotificationSubscriber> notificationSubscribers = _notificationSubscriberRepository.GetForNotificationType(notificationType.Id).ToList(); notificationSubscribers.Add(GetUserToNotify(userId)); if (useAlternateEmail) { var subscriber = GetAlternateEamilToNotify(userId); if (subscriber != null) { notificationSubscribers.Add(subscriber); } } Email fromEmail = _settings.EmailNotificationIssuedFrom; string fromName = _settings.NameNotificationissuedFrom; var notifications = new List <Notification>(); foreach (var notificationSubscriber in notificationSubscribers) { EmailMessage formatMessage = _emailTemplateFormatter.FormatMessage(emailTemplate, model, notificationSubscriber.Email.ToString(), fromEmail.ToString(), fromName, emailTemplate.Id); NotificationEmail notificationEmail; if (notificationDate != null) { notificationEmail = _notificationEmailFactory.CreateNotificationEmail(notificationType, notificationSubscriber, fromEmail, fromName, formatMessage, source, notes, priority, requestedBy, userId, notificationDate.Value); } else { notificationEmail = _notificationEmailFactory.CreateNotificationEmail(notificationType, notificationSubscriber, fromEmail, fromName, formatMessage, source, notes, priority, requestedBy, userId); } Notification notification = _notificationRepository.Save(notificationEmail); notifications.Add(notification); } return(notifications); }
public void ReplacesRazorInBothSubjectAndBody() { var emailTemplate = new EmailTemplate { Subject = "@Model.Subject Hello", Body = "@if (Model.IsTrue) { <p>@Model.TrueMessage</p> } else { <p>@Model.FalseMessage</p> }" }; var model = new { Subject = "Integration Test", TrueMessage = "It is true", FalseMessage = "A lie was told", IsTrue = true }; EmailMessage formatMessage = _emailTemplateFormatter.FormatMessage(emailTemplate, model, "*****@*****.**", "*****@*****.**", "From Me", emailTemplate.Id); Assert.AreEqual(model.Subject + " Hello", formatMessage.Subject); Assert.IsTrue(formatMessage.Body.Contains(model.TrueMessage)); Assert.IsFalse(formatMessage.Body.Contains(model.FalseMessage)); }