Files
contact/Pages/Contact.cshtml.cs
2022-05-06 19:15:02 +00:00

184 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using System.Net.Mail;
using System.Net;
//for captcha:
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
namespace contact.Pages
{
[BindProperties(SupportsGet = true)]
public class ContactFormModel
{
public ContactFormModel()
{
DbId = "n/a v7";
}
[Required]
public string Name { get; set; }
[Required]
public string Company { get; set; }
[BindProperty(SupportsGet = true)]
public string DbId { get; set; }
[Required, EmailAddress]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
public class ContactModel : PageModel
{
public string Message { get; set; }
[BindProperty(SupportsGet = true)]
public ContactFormModel Contact { get; set; }
private readonly IConfiguration _configuration;
public ContactModel(IConfiguration configuration)
{
_configuration = configuration;
}
public ActionResult OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
if (!ReCaptchaPassed(
Request.Form["g-recaptcha-response"], // that's how you get it from the Request object
_configuration.GetSection("GoogleReCaptcha:secret").Value
))
{
//Return a fail code that will hopefully take us off the spammers list
return StatusCode(500);
}
//fuck those spammers
//2022-05-06 changes to get rid of jumbo lead magnet spammer but still force him to pay someone to manually enter the stuff
//ideally it should read from a text file here but this is a quickie fix while Joyce is on important viha phone call
var blocks = new List<string>();
blocks.Add("google");
blocks.Add("jumboleadmagnet");//<--fuck this guy in particular, keeps spamming
var companyname = Contact.Company.ToLowerInvariant();
foreach (string s in blocks)
{
if (companyname.Contains(s))
{
goto skipspam;
}
}
SendMail();
skipspam:
//TODO: go to a successful submit page on the ayanova site
//return Redirect("/confirm.html");//sample quickie page I made up
return Redirect("https://ayanova.com/confirmed.htm");
}
private void SendMail()
{
var MessageBody = $"Name:\r\n{Contact.Name}\r\n\r\nCompany:\r\n{Contact.Company}\r\n\r\nDatabase Id:\r\n{Contact.DbId}\r\n\r\nMessage:\r\n{Contact.Message}";
//SEND TO US
using (var message = new MailMessage())
{
message.To.Add(new MailAddress("support@ayanova.com"));
message.From = new MailAddress(Contact.Email);
message.Subject = $"Question about AyaNova from {Contact.Company}";
message.Body = MessageBody;
using (var smtpClient = new SmtpClient("mail.ayanova.com"))
{
smtpClient.Host = "mail.ayanova.com";
smtpClient.Port = 2525;
smtpClient.UseDefaultCredentials = false;
//testing
// smtpClient.EnableSsl=true;
// smtpClient.Port=465;
// smtpClient.DeliveryMethod= SmtpDeliveryMethod.Network;
//NOTE: Do not use the noreply email address to send mail, it crashes the server somehow
//******************************************************************************************************************
//******************************************************************************************************************
//TEST CRASH
// smtpClient.Credentials = new System.Net.NetworkCredential("noreply@ayanova.com", "91768700489f8edd28aa71e3e0f4073eba54ce83c4c1a6a910700fa94094ddfd");
smtpClient.Credentials = new System.Net.NetworkCredential("webmaster@ayanova.com", "c63c17add818fca81cae71a241ea1b552675a86280b7e7e45d36cbf2e8f3bc0e");
//******************************************************************************************************************
//******************************************************************************************************************
smtpClient.Send(message);
}
}
//SEND COPY TO CLIENT
using (var message = new MailMessage())
{
message.To.Add(new MailAddress(Contact.Email));
message.From = new MailAddress("support@ayanova.com");
message.Subject = $"Confirmation that your question about AyaNova has been received from {Contact.Company}";
message.Body = MessageBody;
using (var smtpClient = new SmtpClient("mail.ayanova.com"))
{
smtpClient.Host = "mail.ayanova.com";
smtpClient.Port = 2525;
smtpClient.UseDefaultCredentials = false;
//NOTE: Do not use the noreply email address to send mail, it crashes the server somehow
smtpClient.Credentials = new System.Net.NetworkCredential("webmaster@ayanova.com", "c63c17add818fca81cae71a241ea1b552675a86280b7e7e45d36cbf2e8f3bc0e");
smtpClient.Send(message);
}
}
}
public void OnGet()
{
Message = "Your contact page.";
}
public static bool ReCaptchaPassed(string gRecaptchaResponse, string secret)
{
HttpClient httpClient = new HttpClient();
var res = httpClient.GetAsync($"https://www.google.com/recaptcha/api/siteverify?secret={secret}&response={gRecaptchaResponse}").Result;
if (res.StatusCode != HttpStatusCode.OK)
{
// logger.LogError("Error while sending request to ReCaptcha");
return false;
}
string JSONres = res.Content.ReadAsStringAsync().Result;
dynamic JSONdata = JObject.Parse(JSONres);
if (JSONdata.success != "true")
{
return false;
}
return true;
}
}
}