Email Rules: Improve validation (#5234)

Came across this while testing things and the "Please fill all required fields before testing" message wasn't clear, because the required fields were not marked.

Co-authored-by: Andrew Camilleri <evilkukka@gmail.com>
This commit is contained in:
d11n 2023-08-07 10:10:48 +03:00 committed by GitHub
parent 1eb7c727f3
commit 7dc41ebcea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 32 deletions

View File

@ -10,6 +10,7 @@ using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Models.ServerViewModels;
using BTCPayServer.Services.Mails;
using BTCPayServer.Validation;
using Microsoft.AspNetCore.Mvc;
using MimeKit;
@ -75,33 +76,22 @@ namespace BTCPayServer.Controllers
if (command.StartsWith("test", StringComparison.InvariantCultureIgnoreCase))
{
var rule = vm.Rules[index];
if (string.IsNullOrEmpty(rule.Subject) || string.IsNullOrEmpty(rule.Body) || string.IsNullOrEmpty(rule.To))
try
{
TempData.SetStatusMessageModel(new StatusMessageModel
{
Severity = StatusMessageModel.StatusSeverity.Warning,
Message = "Please fill all required fields before testing"
});
}
else
{
try
{
var emailSettings = blob.EmailSettings;
using var client = await emailSettings.CreateSmtpClient();
var message = emailSettings.CreateMailMessage(MailboxAddress.Parse(rule.To), "(test) " + rule.Subject, rule.Body, true);
await client.SendAsync(message);
await client.DisconnectAsync(true);
TempData[WellKnownTempData.SuccessMessage] = $"Rule email saved and sent to {rule.To}. Please verify you received it.";
var emailSettings = blob.EmailSettings;
using var client = await emailSettings.CreateSmtpClient();
var message = emailSettings.CreateMailMessage(MailboxAddress.Parse(rule.To), "(test) " + rule.Subject, rule.Body, true);
await client.SendAsync(message);
await client.DisconnectAsync(true);
TempData[WellKnownTempData.SuccessMessage] = $"Rule email saved and sent to {rule.To}. Please verify you received it.";
blob.EmailRules = vm.Rules;
store.SetStoreBlob(blob);
await _Repo.UpdateStore(store);
}
catch (Exception ex)
{
TempData[WellKnownTempData.ErrorMessage] = "Error: " + ex.Message;
}
blob.EmailRules = vm.Rules;
store.SetStoreBlob(blob);
await _Repo.UpdateStore(store);
}
catch (Exception ex)
{
TempData[WellKnownTempData.ErrorMessage] = "Error: " + ex.Message;
}
}
else
@ -128,10 +118,18 @@ namespace BTCPayServer.Controllers
{
[Required]
public WebhookEventType Trigger { get; set; }
public bool CustomerEmail { get; set; }
[Required]
[MailboxAddress]
public string To { get; set; }
public string Body { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Body { get; set; }
}
[HttpGet("{storeId}/email-settings")]

View File

@ -27,7 +27,14 @@
</button>
</div>
</div>
<p class="mb-0">Email rules allow BTCPay Server to send customized emails from your store based on events.</p>
@if (!ViewContext.ModelState.IsValid)
{
<div asp-validation-summary="All" class="text-danger"></div>
}
else
{
<p class="mb-0">Email rules allow BTCPay Server to send customized emails from your store based on events.</p>
}
@if (Model.Rules.Any())
{
@ -53,8 +60,8 @@
<div class="form-text">Choose what event sends the email.</div>
</div>
<div class="form-group">
<label asp-for="Rules[index].To" class="form-label">Recipients</label>
<input type="text" asp-for="Rules[index].To" class="form-control"/>
<label asp-for="Rules[index].To" class="form-label" data-required>Recipients</label>
<input type="text" asp-for="Rules[index].To" class="form-control" />
<span asp-validation-for="Rules[index].To" class="text-danger"></span>
<div class="form-text">Who to send the email to. For multiple emails, separate with a comma.</div>
</div>
@ -64,12 +71,12 @@
<span asp-validation-for="Rules[index].CustomerEmail" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Rules[index].Subject" class="form-label" ></label>
<input type="text" asp-for="Rules[index].Subject" class="form-control"/>
<label asp-for="Rules[index].Subject" class="form-label" data-required></label>
<input type="text" asp-for="Rules[index].Subject" class="form-control" />
<span asp-validation-for="Rules[index].Subject" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Rules[index].Body" class="form-label" ></label>
<label asp-for="Rules[index].Body" class="form-label" data-required></label>
<textarea asp-for="Rules[index].Body" class="form-control richtext" rows="4"></textarea>
<span asp-validation-for="Rules[index].Body" class="text-danger"></span>
</div>