Compare commits
3 Commits
c5ff7fe6a8
...
49e27cdf0b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49e27cdf0b | ||
|
|
d487531541 | ||
|
|
307a5b8548 |
@@ -3,6 +3,7 @@ using System.Net;
|
|||||||
using clivelancaster.Models;
|
using clivelancaster.Models;
|
||||||
using MailKit.Net.Smtp;
|
using MailKit.Net.Smtp;
|
||||||
using MimeKit;
|
using MimeKit;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace clivelancaster.Controllers
|
namespace clivelancaster.Controllers
|
||||||
{
|
{
|
||||||
@@ -27,36 +28,44 @@ namespace clivelancaster.Controllers
|
|||||||
var message = new MimeMessage();
|
var message = new MimeMessage();
|
||||||
message.From.Add(new MailboxAddress("Clive", "clivelancaster@gmail.com"));
|
message.From.Add(new MailboxAddress("Clive", "clivelancaster@gmail.com"));
|
||||||
message.To.Add(new MailboxAddress("Clive", "clivelancaster@gmail.com"));
|
message.To.Add(new MailboxAddress("Clive", "clivelancaster@gmail.com"));
|
||||||
message.Subject = model.Subject;
|
message.Subject = "Website: Contact Form - clivelancaster.com";
|
||||||
|
|
||||||
var bodyBuilder = new BodyBuilder
|
var bodyBuilder = new BodyBuilder
|
||||||
{
|
{
|
||||||
TextBody = $"Name: {model.Name}\nEmail: {model.Email}\n\nMessage:\n{model.Message}"
|
TextBody = $"Name: {model.NameIncognito}\nEmail: {model.Email}\n\nSubject: {model.SubjectIncognito}\n\nMessage:\n{model.MessageIncognito}"
|
||||||
};
|
};
|
||||||
|
|
||||||
message.Body = bodyBuilder.ToMessageBody();
|
message.Body = bodyBuilder.ToMessageBody();
|
||||||
|
|
||||||
try
|
// Honeypot Check
|
||||||
|
if (model.Name != null | model.Subject != null | model.Message != null)
|
||||||
{
|
{
|
||||||
using (var client = new SmtpClient())
|
|
||||||
{
|
|
||||||
// Retrieve SMTP settings from appsettings.json
|
|
||||||
var smtpHost = _configuration["SmtpSettings:Host"];
|
|
||||||
int smtpPort = int.Parse(s: _configuration["SmtpSettings:Port"]);
|
|
||||||
var smtpUsername = _configuration["SmtpSettings:Username"];
|
|
||||||
var smtpPassword = _configuration["SmtpSettings:Password"];
|
|
||||||
|
|
||||||
client.Connect(smtpHost, smtpPort, true);
|
|
||||||
client.Authenticate(smtpUsername, smtpPassword);
|
|
||||||
await client.SendAsync(message);
|
|
||||||
client.Disconnect(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
ViewBag.Message = "Message sent successfully!";
|
ViewBag.Message = "Message sent successfully!";
|
||||||
}
|
}
|
||||||
catch
|
else
|
||||||
{
|
{
|
||||||
ViewBag.Message = "Error sending message.";
|
try
|
||||||
|
{
|
||||||
|
using (var client = new SmtpClient())
|
||||||
|
{
|
||||||
|
// Retrieve SMTP settings from appsettings.json
|
||||||
|
var smtpHost = _configuration["SmtpSettings:Host"];
|
||||||
|
int smtpPort = int.Parse(s: _configuration["SmtpSettings:Port"]);
|
||||||
|
var smtpUsername = _configuration["SmtpSettings:Username"];
|
||||||
|
var smtpPassword = _configuration["SmtpSettings:Password"];
|
||||||
|
|
||||||
|
client.Connect(smtpHost, smtpPort, true);
|
||||||
|
client.Authenticate(smtpUsername, smtpPassword);
|
||||||
|
await client.SendAsync(message);
|
||||||
|
client.Disconnect(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewBag.Message = "Message sent successfully!";
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
ViewBag.Message = "Error sending message.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return View();
|
return View();
|
||||||
|
|||||||
@@ -4,21 +4,27 @@ namespace clivelancaster.Models
|
|||||||
{
|
{
|
||||||
public class ContactFormModel
|
public class ContactFormModel
|
||||||
{
|
{
|
||||||
[Required]
|
|
||||||
[Display(Name = "Your Name")]
|
[Display(Name = "Your Name")]
|
||||||
public required string Name { get; set; }
|
public string? Name { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
[EmailAddress]
|
[EmailAddress]
|
||||||
[Display(Name = "Your Email")]
|
[Display(Name = "Your Email")]
|
||||||
public required string Email { get; set; }
|
public required string Email { get; set; }
|
||||||
|
|
||||||
[Required]
|
|
||||||
[Display(Name = "Subject")]
|
[Display(Name = "Subject")]
|
||||||
public required string Subject { get; set; }
|
public string? Subject { get; set; }
|
||||||
|
|
||||||
[Required]
|
|
||||||
[Display(Name = "Message")]
|
[Display(Name = "Message")]
|
||||||
public required string Message { get; set; }
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
[Display(Name = "Your Name")]
|
||||||
|
public string? NameIncognito { get; set; }
|
||||||
|
|
||||||
|
[Display(Name = "Subject")]
|
||||||
|
public string? SubjectIncognito { get; set; }
|
||||||
|
|
||||||
|
[Display(Name = "Message")]
|
||||||
|
public string? MessageIncognito { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,9 +19,10 @@
|
|||||||
|
|
||||||
<form asp-action="Index" method="post">
|
<form asp-action="Index" method="post">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label asp-for="Name"></label>
|
<label asp-for="Name" class="incognito-field"></label>
|
||||||
<input asp-for="Name" class="form-control" />
|
<input asp-for="Name" class="form-control incognito-field" autocomplete="off" tabindex="-1" />
|
||||||
<span asp-validation-for="Name" class="text-danger"></span>
|
<label asp-for="NameIncognito"></label>
|
||||||
|
<input asp-for="NameIncognito" class="form-control" />
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label asp-for="Email"></label>
|
<label asp-for="Email"></label>
|
||||||
@@ -29,14 +30,16 @@
|
|||||||
<span asp-validation-for="Email" class="text-danger"></span>
|
<span asp-validation-for="Email" class="text-danger"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label asp-for="Subject"></label>
|
<label asp-for="Subject" class="incognito-field"></label>
|
||||||
<input asp-for="Subject" class="form-control" />
|
<input asp-for="Subject" class="form-control incognito-field" autocomplete="off" tabindex="-1" />
|
||||||
<span asp-validation-for="Subject" class="text-danger"></span>
|
<label asp-for="SubjectIncognito"></label>
|
||||||
|
<input asp-for="SubjectIncognito" class="form-control" />
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label asp-for="Message"></label>
|
<label asp-for="Message" class="incognito-field"></label>
|
||||||
<textarea asp-for="Message" class="form-control"></textarea>
|
<textarea asp-for="Message" class="form-control incognito-field" autocomplete="off" tabindex="-1"></textarea>
|
||||||
<span asp-validation-for="Message" class="text-danger"></span>
|
<label asp-for="MessageIncognito"></label>
|
||||||
|
<textarea asp-for="MessageIncognito" class="form-control"></textarea>
|
||||||
</div>
|
</div>
|
||||||
<div class="p-2"></div>
|
<div class="p-2"></div>
|
||||||
<button type="submit" class="btn btn-primary">Send Message</button>
|
<button type="submit" class="btn btn-primary">Send Message</button>
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
<a class="nav-link" asp-area="" asp-controller="Home" asp-action="Clients"><i class="bi bi-people-fill"></i> Clients</a>
|
<a class="nav-link" asp-area="" asp-controller="Home" asp-action="Clients"><i class="bi bi-people-fill"></i> Clients</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a href="https://blog.clivelancaster.com" class="nav-link"><i class="bi bi-substack"></i> Blog</a>
|
<a class="nav-link" href="https://blog.clivelancaster.com" style="color:#ffffff8c;" onmouseover="this.style.color='#ffffffbf'" onmouseout="this.style.color='#ffffff8c'"><i class="bi bi-substack"></i> Blog</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" asp-area="" asp-controller="Contact" asp-action="Index"><i class="bi bi-envelope-fill"></i> Contact</a>
|
<a class="nav-link" asp-area="" asp-controller="Contact" asp-action="Index"><i class="bi bi-envelope-fill"></i> Contact</a>
|
||||||
|
|||||||
@@ -20,6 +20,16 @@
|
|||||||
border-color: #0473b3;
|
border-color: #0473b3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.incognito-field {
|
||||||
|
opacity: 0;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
height: 0;
|
||||||
|
width: 0;
|
||||||
|
z-index: -1;
|
||||||
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
: root {
|
: root {
|
||||||
--current-background: var(--dark-background);
|
--current-background: var(--dark-background);
|
||||||
|
|||||||
Reference in New Issue
Block a user