Example #1
0
        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 }));
        }