public void SendEmails(int?additional) { var tag = Db.FetchOrCreateTag(Util.SessionId, Util.UserPeopleId, Db.NextTagId); Db.ExecuteCommand("delete TagPerson where Id = {0}", tag.Id); Db.TagAll(pids, tag); var dt = new DateTime(ticks); foreach (var id in pids) { var vr = new VolRequest { MeetingId = meeting.MeetingId, RequestorId = person.PeopleId, Requested = dt, VolunteerId = id }; meeting.VolRequests.Add(vr); } var qb = Db.ScratchPadCondition(); qb.Reset(); qb.AddNewClause(QueryType.HasMyTag, CompareType.Equal, $"{tag.Id},temp"); meeting.AddEditExtra(Db, "TotalVolunteersNeeded", ((additional ?? 0) + limit).ToString()); qb.Save(DbUtil.Db); var rurl = DbUtil.Db.ServerLink($"/OnlineReg/VolRequestReport/{meeting.MeetingId}/{person.PeopleId}/{dt.Ticks}"); var reportlink = $@"<a href=""{rurl}"">Volunteer Request Status Report</a>"; var list = Db.PeopleFromPidString(org.NotifyIds).ToList(); //list.Insert(0, person); Db.Email(person.FromEmail, list, "Volunteer Commitment for " + org.OrganizationName, $@" <p>{person.Name} has requested volunteers on {meeting.MeetingDate:MMM d} for {meeting.MeetingDate:h:mm tt}.</p> <blockquote> {reportlink} </blockquote>"); // Email subs var m = new MassEmailer(qb.Id); m.Subject = subject; m.Body = message; DbUtil.LogActivity("Emailing Vol Subs"); m.FromName = person.Name; m.FromAddress = person.FromEmail; var eqid = m.CreateQueue(true).Id; var host = Util.Host; // save these from HttpContext to set again inside thread local storage var useremail = Util.UserEmail; var isinroleemailtest = HttpContext.Current.User.IsInRole("EmailTest"); HostingEnvironment.QueueBackgroundWorkItem(ct => { try { var db = DbUtil.Create(host); // set these again inside thread local storage Util.UserEmail = useremail; Util.IsInRoleEmailTest = isinroleemailtest; db.SendPeopleEmail(eqid); } catch (Exception ex) { var ex2 = new Exception("Emailing error for queueid " + eqid, ex); var errorLog = ErrorLog.GetDefault(null); errorLog.Log(new Error(ex2)); var db = DbUtil.Create(host); // set these again inside thread local storage Util.UserEmail = useremail; Util.IsInRoleEmailTest = isinroleemailtest; var equeue = db.EmailQueues.Single(ee => ee.Id == eqid); equeue.Error = ex.Message.Truncate(200); db.SubmitChanges(); } }); }
public ActionResult QueueEmails(MassEmailer m) { if (!m.Subject.HasValue() || !m.Body.HasValue()) { return(Json(new { id = 0, content = "<h2>Both Subject and Body need some text</h2>" })); } if (!User.IsInRole("Admin") && m.Body.Contains("{createaccount}", ignoreCase: true)) { return(Json(new { id = 0, content = "<h2>Only Admin can use {createaccount}</h2>" })); } if (Util.SessionTimedOut()) { Session["massemailer"] = m; return(Content("timeout")); } DbUtil.LogActivity("Emailing people"); if (m.EmailFroms().Count(ef => ef.Value == m.FromAddress) == 0) { return(Json(new { id = 0, content = "No email address to send from" })); } m.FromName = m.EmailFroms().First(ef => ef.Value == m.FromAddress).Text; int id; try { var eq = m.CreateQueue(); if (eq == null) { throw new Exception("No Emails to send (tag does not exist)"); } id = eq.Id; if (eq.SendWhen.HasValue) { return(Json(new { id = 0, content = "<h2>Emails Queued to be Sent</h2>" })); } } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); return(Json(new { id = 0, content = "<h2>Error</h2><p>{0}</p>".Fmt(ex.Message) })); } string host = Util.Host; // save these from HttpContext to set again inside thread local storage var useremail = Util.UserEmail; var isinroleemailtest = User.IsInRole("EmailTest"); System.Threading.Tasks.Task.Factory.StartNew(() => { Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; try { var Db = new CMSDataContext(Util.GetConnectionString(host)); Db.Host = host; var cul = Db.Setting("Culture", "en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); // set these again inside thread local storage Util.UserEmail = useremail; Util.IsInRoleEmailTest = isinroleemailtest; Db.SendPeopleEmail(id); } catch (Exception ex) { var ex2 = new Exception("Emailing error for queueid " + id, ex); ErrorLog errorLog = ErrorLog.GetDefault(null); errorLog.Log(new Error(ex2)); var Db = new CMSDataContext(Util.GetConnectionString(host)); Db.Host = host; var equeue = Db.EmailQueues.Single(ee => ee.Id == id); equeue.Error = ex.Message.Truncate(200); Db.SubmitChanges(); } }); string keepdraft = Request["keepdraft"]; int saveid = Request["saveid"].ToInt(); System.Diagnostics.Debug.Print("Keep: " + keepdraft + " - Save ID: " + saveid); if (keepdraft != "on" && saveid > 0) { DbUtil.ContentDeleteFromID(saveid); } return(Json(new { id = id })); }
public void SendEmails() { var tag = CurrentDatabase.FetchOrCreateTag(CurrentDatabase.CurrentSessionId, CurrentDatabase.UserPeopleId, CurrentDatabase.NextTagId); CurrentDatabase.ExecuteCommand("delete TagPerson where Id = {0}", tag.Id); CurrentDatabase.TagAll(pids, tag); var dt = new DateTime(ticks); foreach (var id in pids) { var vr = new SubRequest { AttendId = attend.AttendId, RequestorId = person.PeopleId, Requested = dt, SubstituteId = id, }; attend.SubRequests.Add(vr); } CurrentDatabase.SubmitChanges(); var qb = CurrentDatabase.ScratchPadCondition(); qb.Reset(); qb.AddNewClause(QueryType.HasMyTag, CompareType.Equal, $"{tag.Id},temp"); attend.Commitment = CmsData.Codes.AttendCommitmentCode.FindSub; qb.Save(CurrentDatabase); var rurl = CurrentDatabase.ServerLink($"/OnlineReg/VolSubReport/{attend.AttendId}/{person.PeopleId}/{dt.Ticks}"); var reportlink = $@"<a href=""{rurl}"">Substitute Status Report</a>"; var list = CurrentDatabase.PeopleFromPidString(org.NotifyIds).ToList(); //list.Insert(0, person); CurrentDatabase.Email(person.FromEmail, list, "Volunteer Substitute Commitment for " + org.OrganizationName, $@" <p>{person.Name} has requested a substitute on {attend.MeetingDate:MMM d} at {attend.MeetingDate:h:mm tt}.</p> <blockquote> {reportlink} </blockquote>"); // Email subs var m = new MassEmailer(qb.Id); m.Subject = subject; m.Body = message; m.FromName = person.Name; m.FromAddress = person.FromEmail; var eqid = m.CreateQueue(transactional: true).Id; string host = CurrentDatabase.Host; // save these from HttpContext to set again inside thread local storage var useremail = Util.UserEmail; var isinroleemailtest = HttpContextFactory.Current.User.IsInRole("EmailTest"); Log("Send Emails"); HostingEnvironment.QueueBackgroundWorkItem(ct => { try { var db = CMSDataContext.Create(host); // set these again inside thread local storage Util.UserEmail = useremail; Util.IsInRoleEmailTest = isinroleemailtest; db.SendPeopleEmail(eqid); } catch (Exception ex) { Log("Email Error"); var ex2 = new Exception("Emailing error for queueid " + eqid, ex); ErrorLog errorLog = ErrorLog.GetDefault(null); errorLog.Log(new Error(ex2)); var db = CMSDataContext.Create(host); // set these again inside thread local storage Util.UserEmail = useremail; Util.IsInRoleEmailTest = isinroleemailtest; var equeue = db.EmailQueues.Single(ee => ee.Id == eqid); equeue.Error = ex.Message.Truncate(200); db.SubmitChanges(); } }); }
public ActionResult QueueEmails(MassEmailer m) { m.Body = GetBody(m.Body); if (UsesGrammarly(m)) { return(Json(new { error = GrammarlyNotAllowed })); } if (TooLarge(m)) { return(Json(new { error = TooLargeError })); } if (!m.Subject.HasValue() || !m.Body.HasValue()) { return(Json(new { id = 0, error = "Both subject and body need some text." })); } if (!User.IsInRole("Admin") && m.Body.Contains("{createaccount}", ignoreCase: true)) { return(Json(new { id = 0, error = "Only Admin can use {createaccount}." })); } if (Util.SessionTimedOut()) { Session["massemailer"] = m; return(Content("timeout")); } DbUtil.LogActivity("Emailing people"); if (m.EmailFroms().Count(ef => ef.Value == m.FromAddress) == 0) { return(Json(new { id = 0, error = "No email address to send from." })); } m.FromName = m.EmailFroms().First(ef => ef.Value == m.FromAddress).Text; int id; try { var eq = m.CreateQueue(); if (eq == null) { throw new Exception("No Emails to send (tag does not exist)"); } id = eq.Id; // If there are additional recipients, add them to the queue if (m.AdditionalRecipients != null) { foreach (var pid in m.AdditionalRecipients) { // Protect against duplicate PeopleIDs ending up in the queue var q3 = from eqt in CurrentDatabase.EmailQueueTos where eqt.Id == eq.Id where eqt.PeopleId == pid select eqt; if (q3.Any()) { continue; } eq.EmailQueueTos.Add(new EmailQueueTo { PeopleId = pid, OrgId = eq.SendFromOrgId, Guid = Guid.NewGuid(), }); } CurrentDatabase.SubmitChanges(); } if (m.RecipientIds != null) { foreach (var pid in m.RecipientIds) { // Protect against duplicate PeopleIDs ending up in the queue var q3 = from eqt in CurrentDatabase.EmailQueueTos where eqt.Id == eq.Id where eqt.PeopleId == pid select eqt; if (q3.Any()) { continue; } eq.EmailQueueTos.Add(new EmailQueueTo { PeopleId = pid, OrgId = eq.SendFromOrgId, Guid = Guid.NewGuid(), }); } CurrentDatabase.SubmitChanges(); } if (eq.SendWhen.HasValue) { return(Json(new { id = 0, content = "Emails queued to be sent." })); } } catch (Exception ex) { ErrorSignal.FromCurrentContext().Raise(ex); return(Json(new { id = 0, error = ex.Message })); } var host = Util.Host; var currorgid = CurrentDatabase.CurrentSessionOrgId; // save these from HttpContext to set again inside thread local storage var userEmail = Util.UserEmail; var isInRoleEmailTest = User.IsInRole("EmailTest"); var isMyDataUser = User.IsInRole("Access") == false; try { ValidateEmailReplacementCodes(CurrentDatabase, m.Body, new MailAddress(m.FromAddress)); } catch (Exception ex) { return(Json(new { error = ex.Message })); } var onlyProspects = m.OnlyProspects; HostingEnvironment.QueueBackgroundWorkItem(ct => { try { var db = DbUtil.Create(host); var cul = CurrentDatabase.Setting("Culture", "en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); // set these again inside thread local storage db.SetCurrentOrgId(currorgid); Util.UserEmail = userEmail; Util.IsInRoleEmailTest = isInRoleEmailTest; Util.IsMyDataUser = isMyDataUser; db.SendPeopleEmail(id, onlyProspects); } catch (Exception ex) { var ex2 = new Exception($"Emailing error for queueid {id} on {host}\n{ex.Message}", ex); var cb = new SqlConnectionStringBuilder(Util.ConnectionString) { InitialCatalog = "ELMAH" }; var errorLog = new SqlErrorLog(cb.ConnectionString) { ApplicationName = "BVCMS" }; errorLog.Log(new Error(ex2)); var db = DbUtil.Create(host); var equeue = db.EmailQueues.Single(ee => ee.Id == id); equeue.Error = ex.Message.Truncate(200); db.SubmitChanges(); } }); return(Json(new { id = id })); }
public void SendEmails(int?additional) { var tag = Db.FetchOrCreateTag(Util.SessionId, Util.UserPeopleId, Db.NextTagId); Db.ExecuteCommand("delete TagPerson where Id = {0}", tag.Id); Db.TagAll(pids, tag); var dt = new DateTime(ticks); foreach (var id in pids) { var vr = new VolRequest { MeetingId = meeting.MeetingId, RequestorId = person.PeopleId, Requested = dt, VolunteerId = id, }; meeting.VolRequests.Add(vr); } var qb = Db.QueryBuilderScratchPad(); qb.CleanSlate(Db); qb.AddNewClause(QueryType.HasMyTag, CompareType.Equal, "{0},temp".Fmt(tag.Id)); meeting.AddEditExtra(Db, "TotalVolunteersNeeded", ((additional ?? 0) + limit).ToString()); Db.SubmitChanges(); var reportlink = @"<a href=""{0}OnlineReg/RequestReport/{1}/{2}/{3}"">Volunteer Request Status Report</a>" .Fmt(Db.CmsHost, meeting.MeetingId, person.PeopleId, dt.Ticks); var list = Db.PeopleFromPidString(org.NotifyIds).ToList(); //list.Insert(0, person); Db.Email(person.FromEmail, list, "Volunteer Commitment for " + org.OrganizationName, @" <p>{0} has requested volunteers on {1:MMM d} for {1:h:mm tt}.</p> <blockquote> {2} </blockquote>".Fmt(person.Name, meeting.MeetingDate, reportlink)); // Email subs var m = new MassEmailer(qb.QueryId, null); m.Subject = subject; m.Body = message; DbUtil.LogActivity("Emailing Vol Subs"); m.FromName = person.Name; m.FromAddress = person.FromEmail; var eqid = m.CreateQueue(transactional: true); string host = Util.Host; // save these from HttpContext to set again inside thread local storage var useremail = Util.UserEmail; var isinroleemailtest = HttpContext.Current.User.IsInRole("EmailTest"); TaskAlias.Factory.StartNew(() => { Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; try { var db = new CMSDataContext(Util.GetConnectionString(host)); db.Host = host; // set these again inside thread local storage Util.UserEmail = useremail; Util.IsInRoleEmailTest = isinroleemailtest; db.SendPeopleEmail(eqid); } catch (Exception ex) { var ex2 = new Exception("Emailing error for queueid " + eqid, ex); ErrorLog errorLog = ErrorLog.GetDefault(null); errorLog.Log(new Error(ex2)); var db = new CMSDataContext(Util.GetConnectionString(host)); db.Host = host; // set these again inside thread local storage Util.UserEmail = useremail; Util.IsInRoleEmailTest = isinroleemailtest; var equeue = db.EmailQueues.Single(ee => ee.Id == eqid); equeue.Error = ex.Message.Truncate(200); db.SubmitChanges(); } }); }
public ActionResult QueueEmails(MassEmailer m) { m.Body = GetBody(m.Body); if (!m.Subject.HasValue() || !m.Body.HasValue()) { return(Json(new { id = 0, error = "Both subject and body need some text." })); } if (!User.IsInRole("Admin") && m.Body.Contains("{createaccount}", ignoreCase: true)) { return(Json(new { id = 0, error = "Only Admin can use {createaccount}." })); } if (Util.SessionTimedOut()) { Session["massemailer"] = m; return(Content("timeout")); } DbUtil.LogActivity("Emailing people"); if (m.EmailFroms().Count(ef => ef.Value == m.FromAddress) == 0) { return(Json(new { id = 0, error = "No email address to send from." })); } m.FromName = m.EmailFroms().First(ef => ef.Value == m.FromAddress).Text; int id; try { var eq = m.CreateQueue(); if (eq == null) { throw new Exception("No Emails to send (tag does not exist)"); } id = eq.Id; if (eq.SendWhen.HasValue) { return(Json(new { id = 0, content = "Emails queued to be sent." })); } } catch (Exception ex) { ErrorSignal.FromCurrentContext().Raise(ex); return(Json(new { id = 0, error = ex.Message })); } var host = Util.Host; // save these from HttpContext to set again inside thread local storage var userEmail = Util.UserEmail; var isInRoleEmailTest = User.IsInRole("EmailTest"); try { ValidateEmailReplacementCodes(DbUtil.Db, m.Body, new MailAddress(m.FromAddress)); } catch (Exception ex) { return(Json(new { error = ex.Message })); } System.Threading.Tasks.Task.Factory.StartNew(() => { Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; try { var db = DbUtil.Create(host); var cul = db.Setting("Culture", "en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul); // set these again inside thread local storage Util.UserEmail = userEmail; Util.IsInRoleEmailTest = isInRoleEmailTest; db.SendPeopleEmail(id); } catch (Exception ex) { var ex2 = new Exception("Emailing error for queueid " + id, ex); var errorLog = ErrorLog.GetDefault(null); errorLog.Log(new Error(ex2)); var db = DbUtil.Create(host); var equeue = db.EmailQueues.Single(ee => ee.Id == id); equeue.Error = ex.Message.Truncate(200); db.SubmitChanges(); } }); return(Json(new { id = id })); }