public IActionResult EditTemplatePost(string id, MailTemplateModel model, string action) { memoryCache?.Remove("Template_" + id); if (action == "delete") { return(EditTemplateDelete(id)); } else if (action == "send") { model.All = Request.Form.ContainsKey("All"); return(EditTemplateSend(id, model.All)); } try { model.Value.Name = model.Value.Name?.Trim(); if (model.Value.Name.Length > 64) { throw new ArgumentException(Resources.NameIsTooLong.FormatHtml(64)); } if (!model.Value.GetListNameAndTemplateName(out string listName, out string templateName) || !MailTemplate.ValidateName(listName) || !MailTemplate.ValidateName(templateName)) { throw new ArgumentException(Resources.TemplateNameInvalidChars.FormatHtml(MailTemplate.FullNameSeparator)); } using (MailDemonDatabase db = dbProvider.GetDatabase()) { if (db.Lists.FirstOrDefault(l => l.Name == listName) == null) { throw new ArgumentException(string.Format(Resources.ListNotFound, listName)); } model.Value.LastModified = DateTime.UtcNow; model.Value.Dirty = true; if (model.Value.Id == 0) { db.Templates.Add(model.Value); } else { db.Update(model.Value); } db.SaveChanges(); } TempData["Message"] = Resources.Success; return(RedirectToAction(nameof(EditTemplate), new { id = model.Value.Name })); } catch (Exception ex) { MailDemonLog.Error(ex); model.Error = true; model.Message = ex.Message; return(View(model)); } }
public IActionResult EditListPost(string id, MailListModel model, string action) { if (action == "delete") { return(EditListDelete(id)); } try { model.Value.Name = model.Value.Name?.Trim(); if (model.Value.Name.Length > 16) { throw new ArgumentException(Resources.NameIsTooLong.FormatHtml(16)); } else if (!model.Value.FromEmailAddress.TryParseEmailAddress(out _)) { throw new ArgumentException(Resources.EmailIsInvalid); } model.Value.Company = model.Value.Company?.Trim(); model.Value.Website = model.Value.Website?.Trim(); if (!MailTemplate.ValidateName(model.Value.Name)) { throw new ArgumentException(Resources.NameInvalidChars); } using (MailDemonDatabase db = dbProvider.GetDatabase()) { MailList existingList = db.Lists.FirstOrDefault(l => l.Name == model.Value.Name); if (existingList != null && (existingList.Name != model.Value.Name || model.Value.Id == 0)) { throw new ArgumentException(Resources.NameCannotChange); } if (model.Value.Id == 0) { db.Lists.Add(model.Value); } else { db.Update(model.Value); } db.SaveChanges(); } TempData["Message"] = Resources.Success; return(RedirectToAction(nameof(EditList), new { id = model.Value.Name })); } catch (Exception ex) { MailDemonLog.Error(ex); model.Error = true; model.Message = ex.Message; return(View(model)); } }
/// <summary> /// Pre subscribe to a mailing list /// </summary> /// <param name="db">DB</param> /// <param name="reg">Registration, receives new registration if success</param> /// <returns>True if success, false if already subscribed</returns> public static bool PreSubscribeToMailingList(this MailDemonDatabase db, ref MailListSubscription reg) { bool result = false; string token = string.Empty; MailListSubscription final = reg; MailListSubscription dbReg = db.Subscriptions.FirstOrDefault(r => r.EmailAddress == final.EmailAddress && r.ListName == final.ListName); if (dbReg != null) { dbReg.Fields.Clear(); foreach (var kv in final.Fields) { dbReg.SetField(kv.Key, kv.Value); } dbReg.Error = final.Error; dbReg.Message = final.Message; dbReg.IPAddress = final.IPAddress; dbReg.MailList = final.MailList; dbReg.TemplateName = final.TemplateName; final = dbReg; } reg = final; if (reg.SubscribeToken == null) { reg.SubscribeToken = Guid.NewGuid().ToString("N"); reg.Expires = DateTime.UtcNow.AddHours(1.0); if (reg.Id == 0) { db.Subscriptions.Add(reg); } else { db.Update(reg); } result = true; } db.SaveChanges(); return(result); }