Files
2026-03-11 11:18:25 -05:00

114 lines
4.8 KiB
C#

using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Serilog;
namespace Landmark.OLA.Application.Services.Email
{
public class EmailTemplateService : IEmailTemplateService
{
private readonly string _embeddedLogoBase64;
public EmailTemplateService(IWebHostEnvironment webHostEnvironment)
{
_embeddedLogoBase64 = LoadEmbeddedLogo(webHostEnvironment);
}
private static string LoadEmbeddedLogo(IWebHostEnvironment webHostEnvironment)
{
if (webHostEnvironment == null)
{
return string.Empty;
}
try
{
var logoPath = Path.Combine(webHostEnvironment.ContentRootPath, "Files", "Logo.png");
if (!File.Exists(logoPath))
{
Log.Warning("Logo not found at {LogoPath}. Email will fall back to remote image.", logoPath);
return string.Empty;
}
var bytes = File.ReadAllBytes(logoPath);
return Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
Log.Warning(ex, "Failed to embed logo from file.");
return string.Empty;
}
}
public string BuildBrandedBody(string contentHtml, string redBannerMessage = "")
{
var topLogoTag = $"<img src=\"data:image/png;base64,{_embeddedLogoBase64}\" width=\"225\" height=\"62\" style=\"width: 225px; height: 62px\">";
var bottomLogoTag = $"<img src=\"data:image/png;base64,{_embeddedLogoBase64}\" alt=\"Landmark Credit Union Logo\" height=\"37\" width=\"135\" style=\"width: 180px; max-height: 50px;\">";
redBannerMessage ??= string.Empty;
var bannerHtml = string.IsNullOrWhiteSpace(redBannerMessage)
? string.Empty
:
"<div class=\"red-banner\">" +
"<span style=\"background-color: #FF0000; color: #FFFFFF; font-weight: bold; display: inline-block; padding: 4px 8px;\">" +
redBannerMessage +
"</span><br><br>" +
"</div>";
var requestBody =
"<html>" +
"<body>" +
"<table align=\"center\" width=\"87%\" style=\"margin: auto;\">" +
"<tbody>" +
"<tr height=\"50\"></tr>" +
"<tr>" +
"<td align=\"center\" style=\"margin: auto; width: 250px\">" +
topLogoTag +
"</td>" +
"</tr>" +
"<tr height=\"30\"></tr>" +
"</tbody>" +
"</table>" +
"<table align=\"center\" width=\"87%\" style=\"margin: auto; color: #005674\">" +
"<tbody>" +
"<tr>" +
"<td style=\"font-size: 75%; font-family: Verdana; padding-top: 10px;\">" +
bannerHtml +
contentHtml +
"</td>" +
"</tr>" +
"<tr height=\"50\"></tr>" +
"</tbody>" +
"</table>" +
"<table style=\"text-align: center; color: #888; mso-color-alt: #888; font-size: 70%; background-color: #eee; width: 100%; position: absolute; bottom: 0; margin: auto;\" align=\"center\">" +
"<tbody>" +
"<tr>" +
"<td height=\"15\" colspan=\"4\">&nbsp;</td>" +
"</tr>" +
"<tr>" +
"<td height=\"100\" width=\"6.5%\">&nbsp;</td>" +
"<td height=\"100\" width=\"15%\">" +
"<div style=\"padding-right: 15px;\">" +
"<div style=\"margin: auto; display: flex; justify-content: center; align-items: center;\">" +
bottomLogoTag +
"</div>" +
"</div>" +
"</td>" +
"<td align=\"left\" style=\"padding-left: 25px; font-style: italic;\">" +
"</td>" +
"<td width=\"6.5%\">&nbsp;</td>" +
"</tr>" +
"<tr>" +
"<td height=\"15\" colspan=\"4\">&nbsp;</td>" +
"</tr>" +
"</tbody>" +
"</table>" +
"</body>" +
"</html>";
return requestBody;
}
}
}