public ActionResult TestEmail(MassEmailer m) { m.Body = GetBody(m.Body); if (Util.SessionTimedOut()) { Session["massemailer"] = m; return Content("timeout"); } if (m.EmailFroms().Count(ef => ef.Value == m.FromAddress) == 0) return Json(new { error = "No email address to send from." }); m.FromName = m.EmailFroms().First(ef => ef.Value == m.FromAddress).Text; var from = Util.FirstAddress(m.FromAddress, m.FromName); var p = DbUtil.Db.LoadPersonById(Util.UserPeopleId.Value); try { ValidateEmailReplacementCodes(DbUtil.Db, m.Body, from); DbUtil.Db.CopySession(); DbUtil.Db.Email(from, p, null, m.Subject, m.Body, false); } catch (Exception ex) { return Json(new { error = ex.Message }); } return Content("Test email sent."); }
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 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 DbUtil.Db.EmailQueueTos where eqt.EmailQueue == eq where eqt.PeopleId == pid select eqt; if (q3.Any()) { continue; } eq.EmailQueueTos.Add(new EmailQueueTo { PeopleId = pid, OrgId = eq.SendFromOrgId.HasValue ? eq.SendFromOrgId : null, Guid = Guid.NewGuid(), }); } DbUtil.Db.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; // 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 }); }
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}")) 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 { id = m.CreateQueue(); if (id == 0) throw new Exception("No Emails to send (tag does not exist)"); if (m.Schedule.HasValue) return Json(new { id = 0, content = "<h2>Emails Queued</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 ActionResult TestEmail(MassEmailer m) { if (Util.SessionTimedOut()) { Session["massemailer"] = m; return Content("timeout"); } if (m.EmailFroms().Count(ef => ef.Value == m.FromAddress) == 0) return Content("No email address to send from"); m.FromName = m.EmailFroms().First(ef => ef.Value == m.FromAddress).Text; var From = Util.FirstAddress(m.FromAddress, m.FromName); var p = DbUtil.Db.LoadPersonById(Util.UserPeopleId.Value); try { DbUtil.Db.Email(From, p, null, m.Subject, m.Body, false); } catch (Exception ex) { return Content("<h2>Error Email Sent</h2>" + ex.Message); } return Content("<h2>Test Email Sent</h2>"); }
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 DbUtil.Db.EmailQueueTos where eqt.EmailQueue == eq where eqt.PeopleId == pid select eqt; if (q3.Any()) { continue; } eq.EmailQueueTos.Add(new EmailQueueTo { PeopleId = pid, OrgId = eq.SendFromOrgId, Guid = Guid.NewGuid(), }); } DbUtil.Db.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; // 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(DbUtil.Db, m.Body, new MailAddress(m.FromAddress)); } catch (Exception ex) { return Json(new { error = ex.Message }); } HostingEnvironment.QueueBackgroundWorkItem(ct => { 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; Util.IsMyDataUser = isMyDataUser; db.SendPeopleEmail(id); } 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 }); }