feat: add Other contact page submission
This commit is contained in:
parent
1a20749809
commit
08eed3c71e
71
OliverBooth/Controllers/ContactController.cs
Normal file
71
OliverBooth/Controllers/ContactController.cs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
using MailKitSimplified.Sender.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Primitives;
|
||||||
|
|
||||||
|
namespace OliverBooth.Controllers;
|
||||||
|
|
||||||
|
[Controller]
|
||||||
|
[Route("contact/submit")]
|
||||||
|
public class ContactController : Controller
|
||||||
|
{
|
||||||
|
private readonly ILogger<ContactController> _logger;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
private readonly IConfigurationSection _destination;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ContactController" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="configuration">The configuration.</param>
|
||||||
|
public ContactController(ILogger<ContactController> logger, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_configuration = configuration;
|
||||||
|
_destination = configuration.GetSection("Mail").GetSection("Destination");
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{_?}")]
|
||||||
|
public IActionResult OnGet(string _)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Method GET for endpoint {Path} is not supported!", Request.Path);
|
||||||
|
return RedirectToPage("/Contact/Index");
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("other")]
|
||||||
|
public async Task<IActionResult> HandleMiscellaneous()
|
||||||
|
{
|
||||||
|
if (!Request.HasFormContentType)
|
||||||
|
{
|
||||||
|
return RedirectToPage("/Contact/Other");
|
||||||
|
}
|
||||||
|
|
||||||
|
IFormCollection form = Request.Form;
|
||||||
|
StringValues name = form["name"];
|
||||||
|
StringValues email = form["email"];
|
||||||
|
StringValues subject = form["subject"];
|
||||||
|
StringValues message = form["message"];
|
||||||
|
|
||||||
|
await using SmtpSender sender = CreateSender();
|
||||||
|
await sender.WriteEmail
|
||||||
|
.To("Oliver Booth", _destination.GetValue<string>("Other"))
|
||||||
|
.From(name, email)
|
||||||
|
.Subject(subject)
|
||||||
|
.BodyHtml(message)
|
||||||
|
.SendAsync();
|
||||||
|
|
||||||
|
TempData["Success"] = true;
|
||||||
|
return RedirectToPage("/Contact/Result");
|
||||||
|
}
|
||||||
|
|
||||||
|
private SmtpSender CreateSender()
|
||||||
|
{
|
||||||
|
IConfigurationSection mailSection = _configuration.GetSection("Mail");
|
||||||
|
string? mailServer = mailSection.GetSection("Server").Value;
|
||||||
|
string? mailUsername = mailSection.GetSection("Username").Value;
|
||||||
|
string? mailPassword = mailSection.GetSection("Password").Value;
|
||||||
|
|
||||||
|
var sender = SmtpSender.Create(mailServer);
|
||||||
|
sender.SetCredential(mailUsername, mailPassword);
|
||||||
|
return sender;
|
||||||
|
}
|
||||||
|
}
|
@ -18,7 +18,7 @@
|
|||||||
can.
|
can.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<form>
|
<form method="post" asp-controller="Contact" asp-action="HandleMiscellaneous">
|
||||||
<input type="hidden" name="contact-type" value="other">
|
<input type="hidden" name="contact-type" value="other">
|
||||||
|
|
||||||
<div class="form-group" style="margin-top: 10px;">
|
<div class="form-group" style="margin-top: 10px;">
|
||||||
|
25
OliverBooth/Pages/Contact/Result.cshtml
Normal file
25
OliverBooth/Pages/Contact/Result.cshtml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
@page
|
||||||
|
@model OliverBooth.Pages.Contact.Result
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Contact";
|
||||||
|
}
|
||||||
|
|
||||||
|
@if (Model.WasSuccessful)
|
||||||
|
{
|
||||||
|
<h1 class="display-4 text-success"><i class="fa-solid fa-circle-check"></i> Sent successfully!</h1>
|
||||||
|
<p>Thank you for getting in touch. I will get back to you as soon as possible.</p>
|
||||||
|
<p>
|
||||||
|
In the meantime, why not check out my <a asp-page="/Blog/Index">blog</a> or
|
||||||
|
<a asp-page="/Projects/Index">portfolio</a>?
|
||||||
|
</p>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<h1 class="display-4 text-danger"><i class="fa-solid fa-circle-xmark"></i> A problem occured</h1>
|
||||||
|
<p>Sorry, something went wrong. This has been logged and if I'll get to it soon.</p>
|
||||||
|
<p>
|
||||||
|
You can <a asp-page="Index">try again</a>, or check out my <a asp-page="/Blog/Index">blog</a> or
|
||||||
|
<a asp-page="/Projects/Index">portfolio</a>!
|
||||||
|
</p>
|
||||||
|
}
|
23
OliverBooth/Pages/Contact/Result.cshtml.cs
Normal file
23
OliverBooth/Pages/Contact/Result.cshtml.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
|
namespace OliverBooth.Pages.Contact;
|
||||||
|
|
||||||
|
public class Result : PageModel
|
||||||
|
{
|
||||||
|
public bool WasSuccessful { get; private set; }
|
||||||
|
|
||||||
|
public IActionResult OnGet()
|
||||||
|
{
|
||||||
|
if (!TempData.ContainsKey("Success"))
|
||||||
|
{
|
||||||
|
return RedirectToPage("/Contact/Index");
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable S1125
|
||||||
|
WasSuccessful = TempData["Success"] is true;
|
||||||
|
#pragma warning restore S1125
|
||||||
|
TempData.Remove("Success");
|
||||||
|
return Page();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user