mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
SendGrid Mail Delivery Provider (#1892)
* add sendgrid mail delivery service * < * remove duplicate code * fix test by using ISendGridClient interface
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Mail;
|
||||
using Bit.Core.Settings;
|
||||
@ -11,32 +10,30 @@ namespace Bit.Core.Services
|
||||
public class MultiServiceMailDeliveryService : IMailDeliveryService
|
||||
{
|
||||
private readonly IMailDeliveryService _sesService;
|
||||
private readonly IMailDeliveryService _postalService;
|
||||
private readonly int _postalPercentage;
|
||||
private readonly IMailDeliveryService _sendGridService;
|
||||
private readonly int _sendGridPercentage;
|
||||
|
||||
private static Random _random = new Random();
|
||||
|
||||
public MultiServiceMailDeliveryService(
|
||||
GlobalSettings globalSettings,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<AmazonSesMailDeliveryService> sesLogger,
|
||||
ILogger<PostalMailDeliveryService> postalLogger)
|
||||
ILogger<SendGridMailDeliveryService> sendGridLogger)
|
||||
{
|
||||
_sesService = new AmazonSesMailDeliveryService(globalSettings, hostingEnvironment, sesLogger);
|
||||
_postalService = new PostalMailDeliveryService(globalSettings, postalLogger, hostingEnvironment,
|
||||
httpClientFactory);
|
||||
_sendGridService = new SendGridMailDeliveryService(globalSettings, hostingEnvironment, sendGridLogger);
|
||||
|
||||
// 2% by default
|
||||
_postalPercentage = (globalSettings.Mail?.PostalPercentage).GetValueOrDefault(2);
|
||||
// disabled by default (-1)
|
||||
_sendGridPercentage = (globalSettings.Mail?.SendGridPercentage).GetValueOrDefault(-1);
|
||||
}
|
||||
|
||||
public async Task SendEmailAsync(MailMessage message)
|
||||
{
|
||||
var roll = _random.Next(0, 99);
|
||||
if (roll < _postalPercentage)
|
||||
if (roll < _sendGridPercentage)
|
||||
{
|
||||
await _postalService.SendEmailAsync(message);
|
||||
await _sendGridService.SendEmailAsync(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,116 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class PostalMailDeliveryService : IMailDeliveryService
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly ILogger<PostalMailDeliveryService> _logger;
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
private readonly string _baseTag;
|
||||
private readonly string _from;
|
||||
private readonly string _reply;
|
||||
|
||||
public PostalMailDeliveryService(
|
||||
GlobalSettings globalSettings,
|
||||
ILogger<PostalMailDeliveryService> logger,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
IHttpClientFactory clientFactory)
|
||||
{
|
||||
var postalDomain = CoreHelpers.PunyEncode(globalSettings.Mail.PostalDomain);
|
||||
var replyToEmail = CoreHelpers.PunyEncode(globalSettings.Mail.ReplyToEmail);
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
_logger = logger;
|
||||
_clientFactory = clientFactory;
|
||||
_baseTag = $"Env_{hostingEnvironment.EnvironmentName}-" +
|
||||
$"Server_{globalSettings.ProjectName?.Replace(' ', '_')}";
|
||||
_from = $"\"{globalSettings.SiteName}\" <no-reply@{postalDomain}>";
|
||||
_reply = $"\"{globalSettings.SiteName}\" <{replyToEmail}>";
|
||||
}
|
||||
|
||||
public async Task SendEmailAsync(Models.Mail.MailMessage message)
|
||||
{
|
||||
var httpClient = _clientFactory.CreateClient("PostalMailDeliveryService");
|
||||
httpClient.DefaultRequestHeaders.Add("X-Server-API-Key", _globalSettings.Mail.PostalApiKey);
|
||||
|
||||
var request = new PostalRequest
|
||||
{
|
||||
subject = message.Subject,
|
||||
from = _from,
|
||||
reply_to = _reply,
|
||||
html_body = message.HtmlContent,
|
||||
to = new List<string>(),
|
||||
tag = _baseTag
|
||||
};
|
||||
foreach (var address in message.ToEmails)
|
||||
{
|
||||
request.to.Add(CoreHelpers.PunyEncode(address));
|
||||
}
|
||||
|
||||
if (message.BccEmails != null)
|
||||
{
|
||||
request.bcc = new List<string>();
|
||||
foreach (var address in message.BccEmails)
|
||||
{
|
||||
request.bcc.Add(CoreHelpers.PunyEncode(address));
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(message.TextContent))
|
||||
{
|
||||
request.plain_body = message.TextContent;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(message.Category))
|
||||
{
|
||||
request.tag = string.Concat(request.tag, "-Cat_", message.Category);
|
||||
}
|
||||
|
||||
var responseMessage = await httpClient.PostAsJsonAsync(
|
||||
$"https://{_globalSettings.Mail.PostalDomain}/api/v1/send/message",
|
||||
request);
|
||||
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
var response = await responseMessage.Content.ReadFromJsonAsync<PostalResponse>();
|
||||
if (response.status != "success")
|
||||
{
|
||||
_logger.LogError("Postal send status was not successful: {0}, {1}",
|
||||
response.status, response.message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError("Postal send failed: {0}", responseMessage.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class PostalRequest
|
||||
{
|
||||
public List<string> to { get; set; }
|
||||
public List<string> cc { get; set; }
|
||||
public List<string> bcc { get; set; }
|
||||
public string tag { get; set; }
|
||||
public string from { get; set; }
|
||||
public string reply_to { get; set; }
|
||||
public string plain_body { get; set; }
|
||||
public string html_body { get; set; }
|
||||
public string subject { get; set; }
|
||||
}
|
||||
|
||||
public class PostalResponse
|
||||
{
|
||||
public string status { get; set; }
|
||||
public string message { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
118
src/Core/Services/Implementations/SendGridMailDeliveryService.cs
Normal file
118
src/Core/Services/Implementations/SendGridMailDeliveryService.cs
Normal file
@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Mail;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SendGrid;
|
||||
using SendGrid.Helpers.Mail;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class SendGridMailDeliveryService : IMailDeliveryService, IDisposable
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly ILogger<SendGridMailDeliveryService> _logger;
|
||||
private readonly ISendGridClient _client;
|
||||
private readonly string _senderTag;
|
||||
private readonly string _replyToEmail;
|
||||
|
||||
public SendGridMailDeliveryService(
|
||||
GlobalSettings globalSettings,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
ILogger<SendGridMailDeliveryService> logger)
|
||||
: this(new SendGridClient(globalSettings.Mail.SendGridApiKey),
|
||||
globalSettings, hostingEnvironment, logger)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// TODO: nothing to dispose
|
||||
}
|
||||
|
||||
public SendGridMailDeliveryService(
|
||||
ISendGridClient client,
|
||||
GlobalSettings globalSettings,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
ILogger<SendGridMailDeliveryService> logger)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(globalSettings.Mail?.SendGridApiKey))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(globalSettings.Mail.SendGridApiKey));
|
||||
}
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
_logger = logger;
|
||||
_client = client;
|
||||
_senderTag = $"Server_{globalSettings.ProjectName?.Replace(' ', '_')}";
|
||||
_replyToEmail = CoreHelpers.PunyEncode(globalSettings.Mail.ReplyToEmail);
|
||||
}
|
||||
|
||||
public async Task SendEmailAsync(MailMessage message)
|
||||
{
|
||||
var msg = new SendGridMessage();
|
||||
msg.SetFrom(new EmailAddress(_replyToEmail, _globalSettings.SiteName));
|
||||
msg.AddTos(message.ToEmails.Select(e => new EmailAddress(CoreHelpers.PunyEncode(e))).ToList());
|
||||
if (message.BccEmails?.Any() ?? false)
|
||||
{
|
||||
msg.AddBccs(message.BccEmails.Select(e => new EmailAddress(CoreHelpers.PunyEncode(e))).ToList());
|
||||
}
|
||||
|
||||
msg.SetSubject(message.Subject);
|
||||
msg.AddContent(MimeType.Text, message.TextContent);
|
||||
msg.AddContent(MimeType.Html, message.HtmlContent);
|
||||
|
||||
msg.AddCategory($"type:{message.Category}");
|
||||
msg.AddCategory($"env:{_hostingEnvironment.EnvironmentName}");
|
||||
msg.AddCategory($"sender:{_senderTag}");
|
||||
|
||||
msg.SetClickTracking(false, false);
|
||||
msg.SetOpenTracking(false);
|
||||
|
||||
if (message.MetaData != null &&
|
||||
message.MetaData.ContainsKey("SendGridBypassListManagement") &&
|
||||
Convert.ToBoolean(message.MetaData["SendGridBypassListManagement"]))
|
||||
{
|
||||
msg.SetBypassListManagement(true);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var success = await SendAsync(msg, false);
|
||||
if (!success)
|
||||
{
|
||||
_logger.LogWarning("Failed to send email. Retrying...");
|
||||
await SendAsync(msg, true);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogWarning(e, "Failed to send email (with exception). Retrying...");
|
||||
await SendAsync(msg, true);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> SendAsync(SendGridMessage message, bool retry)
|
||||
{
|
||||
if (retry)
|
||||
{
|
||||
// wait and try again
|
||||
await Task.Delay(2000);
|
||||
}
|
||||
|
||||
var response = await _client.SendEmailAsync(message);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var responseBody = await response.Body.ReadAsStringAsync();
|
||||
_logger.LogError("SendGrid email sending failed with {0}: {1}", response.StatusCode, responseBody);
|
||||
}
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user