1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

update to sendgrid 9.x

This commit is contained in:
Kyle Spearrin 2017-03-08 21:11:19 -05:00
parent 6b5b9326b6
commit d7eb5f0f7a
2 changed files with 30 additions and 30 deletions

View File

@ -18,7 +18,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="1.1.2" />
<PackageReference Include="Dapper" Version="1.50.2" /> <PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="DataTableProxy" Version="1.2.0" /> <PackageReference Include="DataTableProxy" Version="1.2.0" />
<PackageReference Include="Sendgrid" Version="6.3.4" /> <PackageReference Include="Sendgrid" Version="9.0.12" />
<PackageReference Include="PushSharp" Version="4.0.10" /> <PackageReference Include="PushSharp" Version="4.0.10" />
<PackageReference Include="WindowsAzure.Storage" Version="8.1.1" /> <PackageReference Include="WindowsAzure.Storage" Version="8.1.1" />
<PackageReference Include="Otp.NET" Version="1.0.1" /> <PackageReference Include="Otp.NET" Version="1.0.1" />

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Mail;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.Core.Domains; using Bit.Core.Domains;
using SendGrid; using SendGrid;
using SendGrid.Helpers.Mail;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
@ -19,12 +19,12 @@ namespace Bit.Core.Services
private const string MarketingCategoryName = "Marketing"; private const string MarketingCategoryName = "Marketing";
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
private readonly Web _web; private readonly SendGridClient _client;
public SendGridMailService(GlobalSettings globalSettings) public SendGridMailService(GlobalSettings globalSettings)
{ {
_globalSettings = globalSettings; _globalSettings = globalSettings;
_web = new Web(_globalSettings.Mail.ApiKey); _client = new SendGridClient(_globalSettings.Mail.ApiKey);
} }
public async Task SendWelcomeEmailAsync(User user) public async Task SendWelcomeEmailAsync(User user)
@ -32,10 +32,10 @@ namespace Bit.Core.Services
var message = CreateDefaultMessage(WelcomeTemplateId); var message = CreateDefaultMessage(WelcomeTemplateId);
message.Subject = "Welcome"; message.Subject = "Welcome";
message.AddTo(user.Email); message.AddTo(new EmailAddress(user.Email));
message.SetCategories(new List<string> { AdministrativeCategoryName, "Welcome" }); message.AddCategories(new List<string> { AdministrativeCategoryName, "Welcome" });
await _web.DeliverAsync(message); await _client.SendEmailAsync(message);
} }
public async Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail) public async Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail)
@ -43,12 +43,12 @@ namespace Bit.Core.Services
var message = CreateDefaultMessage(ChangeEmailAlreadyExistsTemplateId); var message = CreateDefaultMessage(ChangeEmailAlreadyExistsTemplateId);
message.Subject = "Your Email Change"; message.Subject = "Your Email Change";
message.AddTo(toEmail); message.AddTo(new EmailAddress(toEmail));
message.AddSubstitution("{{fromEmail}}", new List<string> { fromEmail }); message.AddSubstitution("{{fromEmail}}", fromEmail);
message.AddSubstitution("{{toEmail}}", new List<string> { toEmail }); message.AddSubstitution("{{toEmail}}", toEmail);
message.SetCategories(new List<string> { AdministrativeCategoryName, "Change Email Alrady Exists" }); message.AddCategories(new List<string> { AdministrativeCategoryName, "Change Email Alrady Exists" });
await _web.DeliverAsync(message); await _client.SendEmailAsync(message);
} }
public async Task SendChangeEmailEmailAsync(string newEmailAddress, string token) public async Task SendChangeEmailEmailAsync(string newEmailAddress, string token)
@ -56,12 +56,12 @@ namespace Bit.Core.Services
var message = CreateDefaultMessage(ChangeEmailTemplateId); var message = CreateDefaultMessage(ChangeEmailTemplateId);
message.Subject = "Change Your Email"; message.Subject = "Change Your Email";
message.AddTo(newEmailAddress); message.AddTo(new EmailAddress(newEmailAddress));
message.AddSubstitution("{{token}}", new List<string> { Uri.EscapeDataString(token) }); message.AddSubstitution("{{token}}", Uri.EscapeDataString(token));
message.SetCategories(new List<string> { AdministrativeCategoryName, "Change Email" }); message.AddCategories(new List<string> { AdministrativeCategoryName, "Change Email" });
message.DisableBypassListManagement(); message.SetBypassListManagement(true);
await _web.DeliverAsync(message); await _client.SendEmailAsync(message);
} }
public async Task SendNoMasterPasswordHintEmailAsync(string email) public async Task SendNoMasterPasswordHintEmailAsync(string email)
@ -69,10 +69,10 @@ namespace Bit.Core.Services
var message = CreateDefaultMessage(NoMasterPasswordHintTemplateId); var message = CreateDefaultMessage(NoMasterPasswordHintTemplateId);
message.Subject = "Your Master Password Hint"; message.Subject = "Your Master Password Hint";
message.AddTo(email); message.AddTo(new EmailAddress(email));
message.SetCategories(new List<string> { AdministrativeCategoryName, "No Master Password Hint" }); message.AddCategories(new List<string> { AdministrativeCategoryName, "No Master Password Hint" });
await _web.DeliverAsync(message); await _client.SendEmailAsync(message);
} }
public async Task SendMasterPasswordHintEmailAsync(string email, string hint) public async Task SendMasterPasswordHintEmailAsync(string email, string hint)
@ -80,29 +80,29 @@ namespace Bit.Core.Services
var message = CreateDefaultMessage(MasterPasswordHintTemplateId); var message = CreateDefaultMessage(MasterPasswordHintTemplateId);
message.Subject = "Your Master Password Hint"; message.Subject = "Your Master Password Hint";
message.AddTo(email); message.AddTo(new EmailAddress(email));
message.AddSubstitution("{{hint}}", new List<string> { hint }); message.AddSubstitution("{{hint}}", hint);
message.SetCategories(new List<string> { AdministrativeCategoryName, "Master Password Hint" }); message.AddCategories(new List<string> { AdministrativeCategoryName, "Master Password Hint" });
await _web.DeliverAsync(message); await _client.SendEmailAsync(message);
} }
private SendGridMessage CreateDefaultMessage(string templateId) private SendGridMessage CreateDefaultMessage(string templateId)
{ {
var message = new SendGridMessage var message = new SendGridMessage
{ {
From = new MailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName), From = new EmailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName),
Html = " ", HtmlContent = " ",
Text = " " PlainTextContent = " "
}; };
if(!string.IsNullOrWhiteSpace(templateId)) if(!string.IsNullOrWhiteSpace(templateId))
{ {
message.EnableTemplateEngine(templateId); message.TemplateId = templateId;
} }
message.AddSubstitution("{{siteName}}", new List<string> { _globalSettings.SiteName }); message.AddSubstitution("{{siteName}}", _globalSettings.SiteName);
message.AddSubstitution("{{baseVaultUri}}", new List<string> { _globalSettings.BaseVaultUri }); message.AddSubstitution("{{baseVaultUri}}", _globalSettings.BaseVaultUri);
return message; return message;
} }