feat: add Other contact page submission

This commit is contained in:
Oliver Booth 2023-09-22 14:57:04 +01:00
parent 1a20749809
commit 08eed3c71e
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
4 changed files with 120 additions and 1 deletions

View 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;
}
}

View File

@ -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;">

View 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>
}

View 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();
}
}