public ActionResult SubmitContactUsForm(ContactUs model)
        {
            if (ModelState.IsValid)
            {
                string encodedResponse = Request.Form["g-Recaptcha-Response"];
                var    response        = ReCaptcha.Validate(encodedResponse, SensativeInformation.ReCaptchaKeys.ReCaptchaSecretKey);
                bool   isCaptchaValid  = response.ToLower() == "true"
                        ? true
                        : false;
                if (!isCaptchaValid)
                {
                    ModelState.AddModelError("", "Please verify you are not a robot");
                    return(CurrentUmbracoPage());
                }

                ContactUsEmailModel emailModel = new ContactUsEmailModel();
                emailModel.Info    = model;
                emailModel.BaseUri = UrlHelpers.GetBaseUrl();
                ViewData.Model     = emailModel;
                var html         = RazorHelpers.RenderRazorViewToString("~/Views/Emails/ContactUsEmail.cshtml", ControllerContext, ViewData, TempData);
                var emailService = new EmailService.EmailHelpers();
                emailService.SendEmail("*****@*****.**", "Query from contact form", html);
                TempData["Success"] = "That is winging its way to us now. We will be in contact as soon as we can.";
                return(RedirectToCurrentUmbracoPage());
            }
            return(CurrentUmbracoPage());
        }
Esempio n. 2
0
    public async Task <IActionResult> Contact(IFormCollection collection)
    {
        if (collection == null)
        {
            throw new ArgumentNullException(nameof(collection));
        }

        ViewBag.NavigationZone = NavigationZone.About;

        var model = new ContactModel();

        await TryUpdateModelAsync <ContactModel>(model);

        model.RecaptchaSiteKey = _captchaService.SiteKey;
        model.SubmitAttempted  = true;

        if (ModelState.IsValid)
        {
            model.IsHuman = await _captchaService.VerifyAsync(collection["g-recaptcha-response"]);

            if (!model.IsHuman)
            {
                ModelState.AddModelError("IsHuman", "The Captcha was not solved correctly, please try again");
            }
            else
            {
                try
                {
                    var to      = _config.To;
                    var from    = _config.To;
                    var subject = _config.Subject;

                    var emailModel = new ContactUsEmailModel
                                     (
                        _config.Subject,
                        model.Email,
                        model.FirstName,
                        model.LastName,
                        model.Message
                                     );

                    var body = await _razorRenderer.RenderViewToStringAsync("~/Views/Email/ContactUs.cshtml", emailModel);

                    await _emailService.SendHtmlAsync(to, from, subject, body);

                    model.SubmitSuccess = true;
                }
                catch (Exception ex)
                {
                    Log.LogError(ex, "There was an error sending an email: {ErrorMessage}", ex.Message);

                    model.SubmitSuccess = false;
                }
            }
        }
        else
        {
            LogValidationErrors();
        }

        return(View(model));
    }