private List <EmailInfo> GenerateNotifications(List <Lesson> lessons, Enumerations.NotificationEmailType notificationType) { List <EmailInfo> emailList = new List <EmailInfo>(); if (lessons != null && lessons.Count > 0) { ApplicationContext appContext = new ApplicationContext(this.HttpContext); foreach (var lesson in lessons) { //If this key exists in the web.config, re-direct all emails to that address. string overrideEmailAddress = Utility.SafeGetAppConfigSetting <string>("Debug_OverrideEmailAddress", null); EmailTemplateViewData model = new EmailTemplateViewData(LessonViewModel.ToViewModel(this.HttpContext, lesson), notificationType, appContext, overrideEmailAddress); string emailMessageBody = Utils.RenderPartialViewToString(this, "EmailTemplate", model); EmailInfo emailInfo = new EmailInfo { Body = emailMessageBody, MailTo = model.Redirecting ? model.OverrideMailTo : model.MailTo, Subject = model.Subject }; //Only send 1 email per email address if (!emailList.Where(x => x.MailTo == emailInfo.MailTo).Any()) { emailList.Add(emailInfo); } } } return(emailList); }
public IActionResult SendNotifications(AdminViewData updatedModel) { ApplicationContext appContext = new ApplicationContext(this.Cache); if (ViewData.ModelState["NotificationDays"].Errors.Count == 0) { UserSessionContext userContext = new UserSessionContext(this.HttpContext); LessonBusiness businessManager = new LessonBusiness(DbContext); var notificationType = (Enumerations.NotificationEmailType)updatedModel.EmailNotificationType; List <Lesson> lessons = businessManager.GetLessonsForNotification(notificationType, updatedModel.NotificationDays); if (lessons != null && lessons.Count > 0) { List <EmailInfo> emailList = new List <EmailInfo>(); foreach (var lesson in lessons) { //If this key exists in the web.config, re-direct all eamils to that address. string overrideEmailAddress = Utility.SafeGetAppConfigSetting <string>("Debug_OverrideEmailAddress", null); EmailTemplateViewData model = new EmailTemplateViewData(LessonViewModel.ToViewModel(this.HttpContext, lesson), notificationType, appContext, overrideEmailAddress); string emailMessageBody = Utils.RenderPartialViewToString(this, "EmailTemplate", model); EmailInfo emailInfo = new EmailInfo { Body = emailMessageBody, MailTo = model.Redirecting ? model.OverrideMailTo : model.MailTo, Subject = model.Subject }; emailList.Add(emailInfo); } businessManager.SendEmails(emailList); } this.SetEmailsSent(); return(RedirectPermanent("Index")); } ModelState.Clear(); AddError("X Days is invalid"); return(Index()); }
/// <summary> /// Created strictly for sending assingment of lesson to user /// </summary> /// <param name="lessons"></param> /// <param name="notificationType"></param> /// <returns></returns> private List <EmailInfo> GenerateNotification(List <Lesson> lessons) { List <EmailInfo> emailList = new List <EmailInfo>(); if (lessons != null && lessons.Count > 0) { ApplicationContext appContext = new ApplicationContext(this.HttpContext); UserSessionContext userContext = new UserSessionContext(this.HttpContext); string overrideEmailAddress = Utility.SafeGetAppConfigSetting <string>("Debug_OverrideEmailAddress", null); Lesson template = lessons.FirstOrDefault(); EmailTemplateViewData model = new EmailTemplateViewData(LessonViewModel.ToViewModel(this.HttpContext, template), Enumerations.NotificationEmailType.N10_AssignToUser, appContext, overrideEmailAddress); string htmlBody = "<p>" + model.AssignTo.FirstName + " " + model.AssignTo.LastName + ", you have new lessons waiting to validate from " + userContext.CurrentUser.FirstName + " " + userContext.CurrentUser.LastName + ".</p>"; htmlBody += "<p>Lesson #'s are:<br>"; UrlHelper urlHelper = new UrlHelper(this.ControllerContext); foreach (var lesson in lessons) { string url = Suncor.LessonsLearnedMP.Web.Helpers.HtmlHelpers.ToPublicUrl(urlHelper, (new Uri("Lesson/Edit/" + lesson.Id, UriKind.Relative))); htmlBody += " <a href=" + url + ">" + lesson.Id.ToString() + " </a>"; htmlBody += "<br>"; } htmlBody += "</p>"; htmlBody += "<p>Please <a href=" + Suncor.LessonsLearnedMP.Web.Helpers.HtmlHelpers.ToPublicUrl(urlHelper, (new Uri("Lesson/MyLessons/", UriKind.Relative))) + ">login</a> to review lessons assigned to you.</p>"; //string emailMessageBody = Utils.RenderPartialViewToString(this, "EmailTemplate", model); EmailInfo emailInfo = new EmailInfo { Body = htmlBody, MailTo = model.Redirecting ? model.OverrideMailTo : model.MailTo, Subject = model.Subject }; emailList.Add(emailInfo); } return(emailList); }