feat: add email-api service and email-api-models contract project

New internal service that centralises SMTP email sending.
- email-api-models: SendEmailRequest DTO, IEmailApiClient Refit interface, EmailApiSettings
- email-api: SmtpEmailDispatcher (MailKit), EmailController (POST /api/email/send),
  branded HTML shell wrapper, shared-Files-volume attachment support
- Protected by X-Internal-Api-Key via UseInternalApiKeyProtection()
- No exposed Docker port — internal network only (http://email-api:8080)

Closes #22

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 16:15:59 +03:00
parent 45d991ab5b
commit 26b13f6dbf
10 changed files with 326 additions and 0 deletions
@@ -0,0 +1,111 @@
using EmailApi.Models.Requests;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Options;
using MimeKit;
using Models.Settings;
namespace EmailApi.Services;
public sealed class SmtpEmailDispatcher
{
private readonly SmtpSettings _smtp;
private readonly FileStorageSettings _fileStorage;
private readonly ILogger<SmtpEmailDispatcher> _log;
private readonly string _environmentName;
private static readonly string HtmlShellStart = """
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"></head>
<body style="margin:0;padding:0;background:#f4f4f4;font-family:Arial,Helvetica,sans-serif">
<table width="100%" cellpadding="0" cellspacing="0" style="padding:20px 0">
<tr><td align="center">
<table width="600" cellpadding="0" cellspacing="0"
style="background:#ffffff;border-radius:8px;max-width:600px">
<tr><td style="background:#2c5282;padding:24px 32px;border-radius:8px 8px 0 0">
<h1 style="margin:0;color:#ffffff;font-size:22px;font-weight:600">myAi</h1>
</td></tr>
<tr><td style="padding:32px">
""";
private static readonly string HtmlShellEnd = """
</td></tr>
<tr><td style="background:#f8f9fa;padding:16px 32px;text-align:center;
color:#6c757d;font-size:12px;border-radius:0 0 8px 8px">
Automated message from myAi.
</td></tr>
</table>
</td></tr>
</table>
</body>
</html>
""";
public SmtpEmailDispatcher(
IOptions<SmtpSettings> smtp,
IOptions<FileStorageSettings> fileStorage,
ILogger<SmtpEmailDispatcher> log)
{
_smtp = smtp.Value;
_fileStorage = fileStorage.Value;
_log = log;
_environmentName = Environment.GetEnvironmentVariable("APP_ENVIRONMENT_NAME") ?? "Development";
}
public async Task SendAsync(SendEmailRequest req, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(_smtp.Host))
{
_log.LogWarning("SMTP host not configured — email skipped (to: {To})", string.Join(", ", req.To));
return;
}
var msg = new MimeMessage();
msg.From.Add(MailboxAddress.Parse(_smtp.Username));
foreach (var to in req.To)
msg.To.Add(MailboxAddress.Parse(to));
if (!string.IsNullOrWhiteSpace(req.ReplyTo))
msg.ReplyTo.Add(MailboxAddress.Parse(req.ReplyTo));
msg.Subject = $"[{_environmentName}] {req.Subject}".Trim();
var builder = new BodyBuilder
{
HtmlBody = HtmlShellStart + req.HtmlBody + HtmlShellEnd
};
if (!string.IsNullOrWhiteSpace(req.AttachmentPath))
{
var fullPath = Path.Combine(_fileStorage.Path, req.AttachmentPath);
if (File.Exists(fullPath))
{
builder.Attachments.Add(fullPath);
_log.LogDebug("Attachment added: {Path}", fullPath);
}
else
{
_log.LogWarning("Attachment not found, skipping: {Path}", fullPath);
}
}
msg.Body = builder.ToMessageBody();
_log.LogInformation("Sending email to {Recipients} subject {Subject}",
string.Join(", ", req.To), req.Subject);
using var client = new SmtpClient();
var tls = _smtp.UseStartTls ? SecureSocketOptions.StartTls : SecureSocketOptions.Auto;
await client.ConnectAsync(_smtp.Host, _smtp.Port, tls, ct);
if (!string.IsNullOrWhiteSpace(_smtp.Username))
await client.AuthenticateAsync(_smtp.Username, _smtp.Password, ct);
await client.SendAsync(msg, ct);
await client.DisconnectAsync(true, ct);
_log.LogInformation("Email sent successfully to {Recipients}", string.Join(", ", req.To));
}
}