95 lines
2.5 KiB
C#
95 lines
2.5 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using plushealthtech.Mail;
|
|
|
|
namespace plushealthtech.Pages
|
|
{
|
|
public class ContactModel : PageModel
|
|
{
|
|
private readonly MailSender _mailSender;
|
|
private readonly ILogger<ContactModel> _logger;
|
|
|
|
public ContactModel(MailSender mailSender, ILogger<ContactModel> logger)
|
|
{
|
|
_mailSender = mailSender;
|
|
_logger = logger;
|
|
}
|
|
|
|
[BindProperty]
|
|
[Required]
|
|
[StringLength(200)]
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
[BindProperty]
|
|
[Required]
|
|
[EmailAddress]
|
|
[StringLength(320)]
|
|
public string Email { get; set; } = string.Empty;
|
|
|
|
[BindProperty]
|
|
[StringLength(200)]
|
|
public string? Organization { get; set; }
|
|
|
|
[BindProperty]
|
|
[StringLength(50)]
|
|
public string? Phone { get; set; }
|
|
|
|
[BindProperty]
|
|
[Required]
|
|
[StringLength(5000)]
|
|
public string Message { get; set; } = string.Empty;
|
|
|
|
public string? Success { get; set; }
|
|
|
|
public string? Error { get; set; }
|
|
|
|
public void OnGet()
|
|
{
|
|
Success = null;
|
|
Error = null;
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return Page();
|
|
}
|
|
|
|
var lines = new List<string>
|
|
{
|
|
$"Name: {Name}",
|
|
$"Email: {Email}"
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(Organization))
|
|
{
|
|
lines.Add($"Organization: {Organization}");
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(Phone))
|
|
{
|
|
lines.Add($"Phone: {Phone}");
|
|
}
|
|
|
|
lines.Add(string.Empty);
|
|
lines.Add(Message);
|
|
|
|
try
|
|
{
|
|
await _mailSender.SendAsync($"New contact form message — {Name}", string.Join("\n", lines));
|
|
Success = $"Thanks — your message has been sent. We'll reply to {Email}.";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log the full exception; never surface SMTP details to the user.
|
|
_logger.LogError(ex, "Failed to send contact form message from {Email}", Email);
|
|
Error = "Your message could not be sent right now. Please try again or email us directly.";
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
}
|
|
}
|