Skip to content

Commit

Permalink
send messages to default address if there are no recipients (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Khaperskaia, Anna <[email protected]>
  • Loading branch information
akonit and Khaperskaia, Anna authored Jun 3, 2024
1 parent 22b8635 commit d1a89d2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ Then fill configuration settings:
{
"NotificationSender": {
"Server": "smtp.server.com", -- smtp server host
"RedirectTo": ["[email protected]"] -- this address will be used as single recipient for all messages on non-prod environments
"RedirectTo": ["[email protected]"], -- all messages on non-prod environments will be sent to these addresses, recipients will be listed in message body
"DefaultRecipients": ["[email protected]"] -- if no recipients are provided for a message then these emails will become recipients
}
}
```
Expand Down
5 changes: 5 additions & 0 deletions src/Bss.Platform.Notifications/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ private static IServiceCollection AddEmailSender(
IHostEnvironment hostEnvironment,
NotificationSenderOptions settings)
{
if (settings.DefaultRecipients.Length == 0)
{
throw new ArgumentException("Default email address is not provided");
}

if (hostEnvironment.IsProduction())
{
return services.AddScoped<IEmailSender, EmailSender>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public class NotificationSenderOptions
public int Port { get; set; } = 25;

public string[]? RedirectTo { get; set; }

public string[] DefaultRecipients { get; set; } = [];
}
25 changes: 23 additions & 2 deletions src/Bss.Platform.Notifications/Services/EmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
using Bss.Platform.Notifications.Interfaces;
using Bss.Platform.Notifications.Models;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Bss.Platform.Notifications.Services;

internal class EmailSender(IEnumerable<IMailMessageSender> senders, IAuditService? auditService = null) : IEmailSender
internal class EmailSender(
IEnumerable<IMailMessageSender> senders,
IOptions<NotificationSenderOptions> settings,
ILogger<EmailSender> logger,
IAuditService? auditService = null) : IEmailSender
{
protected NotificationSenderOptions Settings => settings.Value;

public async Task<MailMessage> SendAsync(EmailModel emailModel, CancellationToken token)
{
var message = this.Convert(emailModel);
Expand All @@ -29,7 +38,19 @@ protected virtual MailMessage Convert(EmailModel model)
{
var mailMessage = new MailMessage { Subject = model.Subject, Body = model.Body, From = model.From, IsBodyHtml = true };

AddRange(mailMessage.To, model.To);
if (model.To.Length != 0)
{
AddRange(mailMessage.To, model.To);
}
else
{
AddRange(mailMessage.To, settings.Value.DefaultRecipients.Select(x => new MailAddress(x)));

logger.LogWarning(
"Recipients are not provided for email '{subject}', redirecting to '{default}'",
mailMessage.Subject,
mailMessage.To);
}

if (model.Cc?.Length > 0)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Bss.Platform.Notifications/Services/EmailSenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
using Bss.Platform.Notifications.Interfaces;
using Bss.Platform.Notifications.Models;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Bss.Platform.Notifications.Services;

internal class EmailSenderTest(
IEnumerable<IMailMessageSender> senders,
IOptions<NotificationSenderOptions> settings,
ILogger<EmailSenderTest> logger,
IAuditService? auditService = null)
: EmailSender(senders, auditService)
: EmailSender(senders, settings, logger, auditService)
{
protected override MailMessage Convert(EmailModel model)
{
Expand All @@ -27,7 +29,7 @@ private void ChangeRecipients(MailMessage message)

ClearRecipients(message);

foreach (var address in settings.Value.RedirectTo!.Select(z => z.Trim()).Distinct().Select(z => new MailAddress(z)))
foreach (var address in this.Settings.RedirectTo!.Select(z => z.Trim()).Distinct().Select(z => new MailAddress(z)))
{
message.To.Add(address);
}
Expand Down
6 changes: 3 additions & 3 deletions src/__SolutionItems/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[assembly: AssemblyCompany("Luxoft")]
[assembly: AssemblyCopyright("Copyright © Luxoft 2024")]

[assembly: AssemblyVersion("1.5.1.0")]
[assembly: AssemblyFileVersion("1.5.1.0")]
[assembly: AssemblyInformationalVersion("1.5.1.0")]
[assembly: AssemblyVersion("1.5.2.0")]
[assembly: AssemblyFileVersion("1.5.2.0")]
[assembly: AssemblyInformationalVersion("1.5.2.0")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
Expand Down

0 comments on commit d1a89d2

Please sign in to comment.