Fixed the bug of email template not found in production environment

This commit is contained in:
ashutosh.nehete 2025-04-27 11:48:56 +05:30
parent 512a899310
commit e6ecbc2f78
2 changed files with 10 additions and 3 deletions

View File

@ -44,5 +44,10 @@
<ProjectReference Include="..\Marco.Pms.Model\Marco.Pms.Model.csproj" /> <ProjectReference Include="..\Marco.Pms.Model\Marco.Pms.Model.csproj" />
<ProjectReference Include="..\Marco.Pms.Utility\Marco.Pms.Utility.csproj" /> <ProjectReference Include="..\Marco.Pms.Utility\Marco.Pms.Utility.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="EmailTemplates\**\*.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project> </Project>

View File

@ -10,16 +10,18 @@ namespace MarcoBMS.Services.Service
{ {
private readonly SmtpSettings _smtpSettings; private readonly SmtpSettings _smtpSettings;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;
public EmailSender(IOptions<SmtpSettings> emailSettings, IConfiguration configuration) public EmailSender(IOptions<SmtpSettings> emailSettings, IConfiguration configuration, IWebHostEnvironment env)
{ {
_smtpSettings = emailSettings.Value; _smtpSettings = emailSettings.Value;
_configuration = configuration; _configuration = configuration;
_env = env;
} }
public async Task<string> GetEmailTemplate(string templateName, Dictionary<string, string> replacements) public async Task<string> GetEmailTemplate(string templateName, Dictionary<string, string> replacements)
{ {
string path = Path.Combine(Directory.GetCurrentDirectory(), "EmailTemplates", $"{templateName}.html"); string path = Path.Combine(_env.ContentRootPath, "EmailTemplates", $"{templateName}.html");
if (!File.Exists(path)) if (!File.Exists(path))
throw new FileNotFoundException("Template file not found"); throw new FileNotFoundException("Template file not found");
@ -52,7 +54,7 @@ namespace MarcoBMS.Services.Service
await SendEmailAsync(toEmails, "New user registration, Reset Your Password", emailBody); await SendEmailAsync(toEmails, "New user registration, Reset Your Password", emailBody);
} }
public async Task SendResetPasswordEmail(string toEmail, string userName, string resetLink) public async Task SendResetPasswordEmail(string toEmail, string userName, string resetLink)
{ {
var replacements = new Dictionary<string, string> var replacements = new Dictionary<string, string>
{ {
{ "MAIL_TITLE", "Reset Your Password" }, { "MAIL_TITLE", "Reset Your Password" },