Files
server/src/Core/Services/Implementations/AmazonSesMailDeliveryService.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

148 lines
5.2 KiB
C#
Raw Normal View History

2019-03-13 14:07:08 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using Bit.Core.Models.Mail;
using Bit.Core.Settings;
using Bit.Core.Utilities;
2019-03-13 14:07:08 -04:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
namespace Bit.Core.Services
{
public class AmazonSesMailDeliveryService : IMailDeliveryService, IDisposable
{
private readonly GlobalSettings _globalSettings;
2020-01-10 08:47:58 -05:00
private readonly IWebHostEnvironment _hostingEnvironment;
2019-03-13 14:07:08 -04:00
private readonly ILogger<AmazonSesMailDeliveryService> _logger;
private readonly IAmazonSimpleEmailService _client;
2019-03-13 14:07:08 -04:00
private readonly string _source;
private readonly string _senderTag;
2019-03-13 16:19:00 -04:00
private readonly string _configSetName;
2019-03-13 14:07:08 -04:00
public AmazonSesMailDeliveryService(
GlobalSettings globalSettings,
2020-01-10 08:47:58 -05:00
IWebHostEnvironment hostingEnvironment,
2019-03-13 14:07:08 -04:00
ILogger<AmazonSesMailDeliveryService> logger)
2020-06-18 09:57:24 -04:00
: this(globalSettings, hostingEnvironment, logger,
new AmazonSimpleEmailServiceClient(
globalSettings.Amazon.AccessKeyId,
globalSettings.Amazon.AccessKeySecret,
RegionEndpoint.GetBySystemName(globalSettings.Amazon.Region))
)
{
}
public AmazonSesMailDeliveryService(
GlobalSettings globalSettings,
IWebHostEnvironment hostingEnvironment,
ILogger<AmazonSesMailDeliveryService> logger,
IAmazonSimpleEmailService amazonSimpleEmailService)
2019-03-13 14:07:08 -04:00
{
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeyId))
2019-03-13 14:07:08 -04:00
{
throw new ArgumentNullException(nameof(globalSettings.Amazon.AccessKeyId));
}
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeySecret))
2019-03-13 14:07:08 -04:00
{
throw new ArgumentNullException(nameof(globalSettings.Amazon.AccessKeySecret));
}
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.Region))
2019-03-13 14:07:08 -04:00
{
throw new ArgumentNullException(nameof(globalSettings.Amazon.Region));
}
var replyToEmail = CoreHelpers.PunyEncode(globalSettings.Mail.ReplyToEmail);
2019-03-13 14:07:08 -04:00
_globalSettings = globalSettings;
_hostingEnvironment = hostingEnvironment;
_logger = logger;
_client = amazonSimpleEmailService;
_source = $"\"{globalSettings.SiteName}\" <{replyToEmail}>";
_senderTag = $"Server_{globalSettings.ProjectName?.Replace(' ', '_')}";
if (!string.IsNullOrWhiteSpace(_globalSettings.Mail.AmazonConfigSetName))
2019-03-13 16:19:00 -04:00
{
_configSetName = _globalSettings.Mail.AmazonConfigSetName;
}
2019-03-13 14:07:08 -04:00
}
public void Dispose()
{
_client?.Dispose();
}
public async Task SendEmailAsync(MailMessage message)
{
var request = new SendEmailRequest
{
2019-03-13 16:19:00 -04:00
ConfigurationSetName = _configSetName,
2019-03-13 14:07:08 -04:00
Source = _source,
Destination = new Destination
{
ToAddresses = message.ToEmails
.Select(email => CoreHelpers.PunyEncode(email))
.ToList()
2019-03-13 14:07:08 -04:00
},
Message = new Message
{
Subject = new Content(message.Subject),
Body = new Body
{
Html = new Content
{
Charset = "UTF-8",
Data = message.HtmlContent
},
Text = new Content
{
Charset = "UTF-8",
Data = message.TextContent
}
}
},
Tags = new List<MessageTag>
{
new MessageTag { Name = "Environment", Value = _hostingEnvironment.EnvironmentName },
new MessageTag { Name = "Sender", Value = _senderTag }
}
};
if (message.BccEmails?.Any() ?? false)
2019-03-13 14:07:08 -04:00
{
request.Destination.BccAddresses = message.BccEmails
.Select(email => CoreHelpers.PunyEncode(email))
.ToList();
2019-03-13 14:07:08 -04:00
}
if (!string.IsNullOrWhiteSpace(message.Category))
2019-03-13 14:07:08 -04:00
{
2019-03-13 16:19:00 -04:00
request.Tags.Add(new MessageTag { Name = "Category", Value = message.Category });
2019-03-13 14:07:08 -04:00
}
try
{
await SendAsync(request, false);
}
catch (Exception e)
2019-03-13 14:07:08 -04:00
{
2021-07-07 22:40:47 -04:00
_logger.LogWarning(e, "Failed to send email. Retrying...");
2019-03-13 14:07:08 -04:00
await SendAsync(request, true);
throw;
2019-03-13 14:07:08 -04:00
}
}
private async Task SendAsync(SendEmailRequest request, bool retry)
{
if (retry)
2019-03-13 14:07:08 -04:00
{
// wait and try again
await Task.Delay(2000);
}
await _client.SendEmailAsync(request);
}
}
}