From 08eed3c71e7863bc86db20a393e8cef2f5a951cc Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 22 Sep 2023 14:57:04 +0100 Subject: [PATCH] feat: add Other contact page submission --- OliverBooth/Controllers/ContactController.cs | 71 ++++++++++++++++++++ OliverBooth/Pages/Contact/Other.cshtml | 2 +- OliverBooth/Pages/Contact/Result.cshtml | 25 +++++++ OliverBooth/Pages/Contact/Result.cshtml.cs | 23 +++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 OliverBooth/Controllers/ContactController.cs create mode 100644 OliverBooth/Pages/Contact/Result.cshtml create mode 100644 OliverBooth/Pages/Contact/Result.cshtml.cs diff --git a/OliverBooth/Controllers/ContactController.cs b/OliverBooth/Controllers/ContactController.cs new file mode 100644 index 0000000..0f11dc5 --- /dev/null +++ b/OliverBooth/Controllers/ContactController.cs @@ -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 _logger; + private readonly IConfiguration _configuration; + private readonly IConfigurationSection _destination; + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + /// The configuration. + public ContactController(ILogger 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 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("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; + } +} diff --git a/OliverBooth/Pages/Contact/Other.cshtml b/OliverBooth/Pages/Contact/Other.cshtml index c580480..ad08cc7 100644 --- a/OliverBooth/Pages/Contact/Other.cshtml +++ b/OliverBooth/Pages/Contact/Other.cshtml @@ -18,7 +18,7 @@ can.

-
+
diff --git a/OliverBooth/Pages/Contact/Result.cshtml b/OliverBooth/Pages/Contact/Result.cshtml new file mode 100644 index 0000000..2268664 --- /dev/null +++ b/OliverBooth/Pages/Contact/Result.cshtml @@ -0,0 +1,25 @@ +@page +@model OliverBooth.Pages.Contact.Result + +@{ + ViewData["Title"] = "Contact"; +} + +@if (Model.WasSuccessful) +{ +

Sent successfully!

+

Thank you for getting in touch. I will get back to you as soon as possible.

+

+ In the meantime, why not check out my blog or + portfolio? +

+} +else +{ +

A problem occured

+

Sorry, something went wrong. This has been logged and if I'll get to it soon.

+

+ You can try again, or check out my blog or + portfolio! +

+} diff --git a/OliverBooth/Pages/Contact/Result.cshtml.cs b/OliverBooth/Pages/Contact/Result.cshtml.cs new file mode 100644 index 0000000..5d3179c --- /dev/null +++ b/OliverBooth/Pages/Contact/Result.cshtml.cs @@ -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(); + } +}