mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-10-07 10:28:55 +00:00
87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
|
|
using Amazon.SimpleEmailV2;
|
||
|
|
using Amazon.SimpleEmailV2.Model;
|
||
|
|
using Microsoft.Extensions.Configuration;
|
||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
using ProposalSystem.Application.Interfaces;
|
||
|
|
|
||
|
|
namespace ProposalSystem.Infrastructure.Services;
|
||
|
|
|
||
|
|
public class SesEmailService : IEmailService
|
||
|
|
{
|
||
|
|
private readonly IAmazonSimpleEmailServiceV2 _ses;
|
||
|
|
private readonly string _fromAddress;
|
||
|
|
private readonly ILogger<SesEmailService> _logger;
|
||
|
|
|
||
|
|
public SesEmailService(
|
||
|
|
IAmazonSimpleEmailServiceV2 ses,
|
||
|
|
IConfiguration config,
|
||
|
|
ILogger<SesEmailService> logger)
|
||
|
|
{
|
||
|
|
_ses = ses;
|
||
|
|
_fromAddress = config["SES_FROM_ADDRESS"]
|
||
|
|
?? throw new InvalidOperationException(
|
||
|
|
"SES_FROM_ADDRESS environment variable is required for email delivery.");
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task SendProposalDeliveryAsync(
|
||
|
|
string toEmail,
|
||
|
|
string proposalNumber,
|
||
|
|
string customerName,
|
||
|
|
string pdfUrl,
|
||
|
|
CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var subject = $"Your proposal {proposalNumber} from Sea Haven Industries";
|
||
|
|
|
||
|
|
var htmlBody = $@"
|
||
|
|
<html>
|
||
|
|
<body style=""font-family: Arial, sans-serif; color: #333;"">
|
||
|
|
<p>Dear {System.Net.WebUtility.HtmlEncode(customerName)},</p>
|
||
|
|
<p>Your proposal <strong>{System.Net.WebUtility.HtmlEncode(proposalNumber)}</strong> is ready for review.</p>
|
||
|
|
<p>
|
||
|
|
<a href=""{System.Net.WebUtility.HtmlEncode(pdfUrl)}"" style=""display: inline-block; padding: 10px 20px; background-color: #0066cc; color: #ffffff; text-decoration: none; border-radius: 4px;"">View Proposal PDF</a>
|
||
|
|
</p>
|
||
|
|
<p>This link will expire in 7 days. If you have any questions, please contact us directly.</p>
|
||
|
|
<p>Thank you,<br/>Sea Haven Industries</p>
|
||
|
|
</body>
|
||
|
|
</html>";
|
||
|
|
|
||
|
|
var textBody = $@"Dear {customerName},
|
||
|
|
|
||
|
|
Your proposal {proposalNumber} is ready for review.
|
||
|
|
|
||
|
|
View the proposal PDF at: {pdfUrl}
|
||
|
|
|
||
|
|
This link will expire in 7 days. If you have any questions, please contact us directly.
|
||
|
|
|
||
|
|
Thank you,
|
||
|
|
Sea Haven Industries";
|
||
|
|
|
||
|
|
var request = new SendEmailRequest
|
||
|
|
{
|
||
|
|
FromEmailAddress = _fromAddress,
|
||
|
|
Destination = new Destination
|
||
|
|
{
|
||
|
|
ToAddresses = new List<string> { toEmail },
|
||
|
|
},
|
||
|
|
Content = new EmailContent
|
||
|
|
{
|
||
|
|
Simple = new Message
|
||
|
|
{
|
||
|
|
Subject = new Content { Data = subject },
|
||
|
|
Body = new Body
|
||
|
|
{
|
||
|
|
Html = new Content { Data = htmlBody },
|
||
|
|
Text = new Content { Data = textBody },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
await _ses.SendEmailAsync(request, ct);
|
||
|
|
_logger.LogInformation(
|
||
|
|
"Proposal delivery email sent for {ProposalNumber}",
|
||
|
|
proposalNumber);
|
||
|
|
}
|
||
|
|
}
|