Ejemplo n.º 1
0
        public IActionResult SubscribeInitial(string id)
        {
            string result = TempData["result"] as string;

            id = (id ?? string.Empty).Trim();
            if (id.Length == 0)
            {
                return(NotFound());
            }
            MailListSubscription model = (string.IsNullOrWhiteSpace(result) ? new MailListSubscription() : JsonConvert.DeserializeObject <MailListSubscription>(result));

            using (MailDemonDatabase db = dbProvider.GetDatabase())
            {
                model.MailList = db.Lists.FirstOrDefault(l => l.Name == id);
            }
            if (model.MailList == null)
            {
                return(NotFound());
            }
            model.ListName     = id;
            model.TemplateName = MailTemplate.GetFullTemplateName(id, MailTemplate.NameSubscribeInitial);
            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SubscribeWelcome(string id, string token)
        {
            id = (id ?? string.Empty).Trim();
            if (id.Length == 0)
            {
                return(NotFound());
            }

            // stupid bing/outlook email preview
            string userAgent = Request.Headers["User-Agent"].ToString();

            if (string.IsNullOrWhiteSpace(userAgent) || userAgent.Contains("preview", StringComparison.OrdinalIgnoreCase))
            {
                return(Content(string.Empty));
            }

            token = (token ?? string.Empty).Trim();
            MailListSubscription reg;

            using (MailDemonDatabase db = dbProvider.GetDatabase())
            {
                reg = db.ConfirmSubscribeToMailingList(id, token);
            }
            if (reg == null)
            {
                return(View("SubscribeExpired"));
            }

            // temp property does not go in db
            reg.UnsubscribeUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/{nameof(Unsubscribe)}/{id}?token={reg.UnsubscribeToken}";

            string templateFullName = MailTemplate.GetFullTemplateName(id, MailTemplate.NameSubscribeWelcome);

            await SendMailAsync(reg, templateFullName);

            return(View(reg));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> SubscribeInitialPost(string id, Dictionary <string, string> formFields)
        {
            id = (id ?? string.Empty).Trim();
            if (id.Length == 0)
            {
                return(NotFound());
            }
            string error = null;

            if (RequireCaptcha)
            {
                formFields.TryGetValue("captcha", out string captchaValue);
                error = await MailDemonWebApp.Instance.Recaptcha.Verify(Request.GetDisplayUrl(), captchaValue, nameof(SubscribeInitial), HttpContext.GetRemoteIPAddress().ToString(), formFields);
            }
            MailListSubscription model = new MailListSubscription {
                Message = error, Error = !string.IsNullOrWhiteSpace(error)
            };
            MailList list;

            using (MailDemonDatabase db = dbProvider.GetDatabase())
            {
                list = db.Lists.FirstOrDefault(l => l.Name == id);
            }
            if (list == null)
            {
                return(NotFound());
            }
            model.MailList = list;
            model.ListName = model.MailList.Name;
            if (formFields.ContainsKey("TemplateName"))
            {
                model.TemplateName = formFields["TemplateName"];
            }
            string email = null;

            foreach (KeyValuePair <string, string> field in formFields)
            {
                if (field.Key.StartsWith("ff_"))
                {
                    string value = field.Value?.Trim();
                    string name  = field.Key.Split('_')[1];
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        if (field.Key.EndsWith("_optional", StringComparison.OrdinalIgnoreCase))
                        {
                            model.Fields[name] = string.Empty;
                        }
                        else
                        {
                            model.Message += "<br/>" + field.Key.Split('_')[1] + " is required";
                            model.Error    = true;
                        }
                    }
                    else if (name.Contains("email", StringComparison.OrdinalIgnoreCase))
                    {
                        if (value.TryParseEmailAddress(out _))
                        {
                            email = (email ?? value);
                            model.EmailAddress = email;
                        }
                        else
                        {
                            model.Error = true;
                        }
                    }
                    else
                    {
                        model.Fields[name] = value;
                    }
                }
            }
            model.EmailAddressDomain = model.GetDomainFromEmailAddress();
            TempData["result"]       = JsonConvert.SerializeObject(model);
            if (model.Error || email == null)
            {
                if (email == null)
                {
                    model.Error    = true;
                    model.Message += "<br/>" + Resources.EmailIsInvalid;
                }
                return(View(nameof(SubscribeInitial), model));
            }
            else
            {
                try
                {
                    model.IPAddress = HttpContext.GetRemoteIPAddress().ToString();
                    using (MailDemonDatabase db = dbProvider.GetDatabase())
                    {
                        if (!db.PreSubscribeToMailingList(ref model))
                        {
                            throw new InvalidOperationException(Resources.AlreadySubscribed.FormatHtml(id));
                        }
                    }
                    string url = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/{nameof(SubscribeWelcome)}/{id}?token={model.SubscribeToken}";
                    model.SubscribeUrl = url;
                    string templateFullName = MailTemplate.GetFullTemplateName(id, MailTemplate.NameSubscribeConfirm);
                    await SendMailAsync(model, templateFullName);

                    return(RedirectToAction(nameof(SubscribeConfirm), new { id = model.ListName }));
                }
                catch (Exception ex)
                {
                    MailDemonLog.Error(ex);
                    model.Error    = true;
                    model.Message += "<br/>" + ex.Message;
                    return(View(nameof(SubscribeInitial), model));
                }
            }
        }