public async Task <JObject> SendFeedback([FromServices] IGoogleRecaptcha googleRecaptcha, [FromServices] IEmailSender mailSender, [FromServices] IHostingEnvironment env) { string name = Request.Form["cname"]; string email = Request.Form["cemail"]; string feedbackMessage = Request.Form["cmessage"]; string encodedResponse = Request.Form["g-recaptcha-response-token"]; string action = Request.Form["g-recaptcha-action"]; string subject = $"{Settings.SiteNameDomain}: Feedback from customer"; bool isCaptchaValid = await googleRecaptcha.IsCaptchaValid(encodedResponse, action); if (!isCaptchaValid && env.IsProduction()) { return(JObject.FromObject(new { success = false })); } var textHtml = $"<p>{Settings.SiteNameDomain}: Feedback from customer with name: <strong>{name}</strong></p><br>" + $"<p>and e-mail: <strong>{email}</strong>: </p><br>" + $"<p>{feedbackMessage}</p>"; try { await mailSender.SendEmailAsync(Settings.SupportEmail, subject, textHtml); return(JObject.FromObject(new { success = true })); } catch { return(JObject.FromObject(new { success = false })); } }
public ContactController( IHostingEnvironment hostingEnvironment, IOptionsMonitor <BlogPostsSettings> blogPostsConfig, IOptions <EmailSettings> emailSettings, IOptions <GoogleRecaptchaSettings> googleRecaptchaSettings, IGoogleRecaptcha googleRecaptcha, IEmailSender emailSender) : base(hostingEnvironment) { _hostingEnvironment = hostingEnvironment; _blogPostsConfig = blogPostsConfig.CurrentValue; _emailSettings = emailSettings.Value; _emailSender = emailSender; _googleRecaptchaSettings = googleRecaptchaSettings.Value; _googleRecaptcha = googleRecaptcha; }