send test email
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using JobTrackerApi.Models;
|
||||
using JobTrackerApi.Models;
|
||||
using JobTrackerApi.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace JobTrackerApi.Controllers;
|
||||
|
||||
@@ -113,6 +114,7 @@ public sealed class UsersController : ControllerBase
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("{id}/send-password-reset")]
|
||||
public async Task<IActionResult> SendPasswordReset([FromRoute] string id, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -139,5 +141,30 @@ public sealed class UsersController : ControllerBase
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record SendTestEmailRequest(string? ToEmail, string? Subject, string? Message);
|
||||
|
||||
[HttpPost("send-test-email")]
|
||||
public async Task<IActionResult> SendTestEmail([FromBody] SendTestEmailRequest? request, CancellationToken cancellationToken)
|
||||
{
|
||||
var currentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub");
|
||||
var currentUser = currentUserId is null ? null : await _users.FindByIdAsync(currentUserId);
|
||||
|
||||
var toEmail = (request?.ToEmail ?? currentUser?.Email ?? "").Trim();
|
||||
if (string.IsNullOrWhiteSpace(toEmail)) return BadRequest("Recipient email is required.");
|
||||
|
||||
var subject = string.IsNullOrWhiteSpace(request?.Subject) ? "Job Tracker test email" : request!.Subject!.Trim();
|
||||
var message = string.IsNullOrWhiteSpace(request?.Message)
|
||||
? "This is a test email from the Job Tracker admin panel.\n\nIf you received this, the SMTP configuration is working."
|
||||
: request!.Message!.Trim();
|
||||
|
||||
await _email.SendAsync(
|
||||
toEmail,
|
||||
subject,
|
||||
$"{message}\n\nSent at: {DateTimeOffset.UtcNow:u}",
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user