diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 53f11fa75c..f5b3bcc7c8 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -6,63 +6,15 @@ false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + diff --git a/src/Core/MailTemplates/Handlebars/ChangeEmail.html.hbs b/src/Core/MailTemplates/Handlebars/ChangeEmail.html.hbs new file mode 100644 index 0000000000..c762c2789c --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/ChangeEmail.html.hbs @@ -0,0 +1,5 @@ +{{#>FullHtmlLayout}} +

+ To finalize changing your email address enter the following code in the pop-up window: {{Token}} +

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/ChangeEmail.text.hbs b/src/Core/MailTemplates/Handlebars/ChangeEmail.text.hbs new file mode 100644 index 0000000000..38ba8741a5 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/ChangeEmail.text.hbs @@ -0,0 +1,3 @@ +{{#>BasicTextLayout}} +To finalize changing your email address enter the following code in the pop-up window: {{Token}} +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/ChangeEmailAlreadyExists.html.hbs b/src/Core/MailTemplates/Handlebars/ChangeEmailAlreadyExists.html.hbs new file mode 100644 index 0000000000..2a51bc0711 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/ChangeEmailAlreadyExists.html.hbs @@ -0,0 +1,9 @@ +{{#>FullHtmlLayout}} +

+ A user ({{FromEmail}}) recently tried to change their account to use this + email address ({{ToEmail}}). An account already exists with this email ({{ToEmail}}). +

+

+ If you did not try to change an email address, you can safely ignore this email. +

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/ChangeEmailAlreadyExists.text.hbs b/src/Core/MailTemplates/Handlebars/ChangeEmailAlreadyExists.text.hbs new file mode 100644 index 0000000000..521b7b90ff --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/ChangeEmailAlreadyExists.text.hbs @@ -0,0 +1,5 @@ +{{#>BasicTextLayout}} +A user ({{FromEmail}}) recently tried to change their account to use this email address ({{ToEmail}}). An account already exists with this email ({{ToEmail}}). + +If you did not try to change an email address, you can safely ignore this email. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/InvoiceUpcoming.html.hbs b/src/Core/MailTemplates/Handlebars/InvoiceUpcoming.html.hbs new file mode 100644 index 0000000000..63120574b8 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/InvoiceUpcoming.html.hbs @@ -0,0 +1,30 @@ +{{#>FullHtmlLayout}} +

+ This is a reminder that your Bitwarden subscription is due for renewal soon. + Your payment method on file will be charged for {{usd AmountDue}} on + {{date DueDate 'MMM dd, yyyy'}}. +

+{{#if Items}} +

+ Summary Of Charges
+ {{#each Items}} + {{this}}
+ {{/each}} +

+{{/if}} +

+ To avoid any interruption in service, please ensure that your payment method + on file is up to date and can be charged for the above amount. You can manage your + subscription and payment method by logging into the web vault at + {{{link WebVaultUrl}}}. Once logged in, navigate to the Billing page for your account. +

+{{#if MentionInvoices}} +

+ Invoices for your payments can also be downloaded from Billing page for your account. +

+{{/if}} +

+ If you have any questions or problems, please feel free to email us at + hello@bitwarden.com. +

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/InvoiceUpcoming.text.hbs b/src/Core/MailTemplates/Handlebars/InvoiceUpcoming.text.hbs new file mode 100644 index 0000000000..281edb29dd --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/InvoiceUpcoming.text.hbs @@ -0,0 +1,19 @@ +{{#>BasicTextLayout}} +This is a reminder that your Bitwarden subscription is due for renewal soon. Your payment method on file will be charged for {{usd AmountDue}} on {{date DueDate 'MMM dd, yyyy'}}. +{{#if Items}} + +Summary Of Charges +------------------ +{{#each Items}} +{{this}} +{{/each}} +{{/if}} + +To avoid any interruption in service, please ensure that your payment method on file is up to date and can be charged for the above amount. You can manage your subscription and payment method by logging into the web vault at {{{WebVaultUrl}}}. Once logged in, navigate to the Billing page for your account. +{{#if MentionInvoices}} + +Invoices for your payments can also be downloaded from Billing page for your account. +{{/if}} + +If you have any questions or problems, please feel free to email us at hello@bitwarden.com. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/Layouts/Basic.html.hbs b/src/Core/MailTemplates/Handlebars/Layouts/Basic.html.hbs new file mode 100644 index 0000000000..f2bf9ca703 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/Layouts/Basic.html.hbs @@ -0,0 +1,10 @@ + + + + + + + + {{>@partial-block}} + + diff --git a/src/Core/MailTemplates/Handlebars/Layouts/Basic.text.hbs b/src/Core/MailTemplates/Handlebars/Layouts/Basic.text.hbs new file mode 100644 index 0000000000..8ac06fdae4 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/Layouts/Basic.text.hbs @@ -0,0 +1 @@ +{{>@partial-block}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/Layouts/Full.html.hbs b/src/Core/MailTemplates/Handlebars/Layouts/Full.html.hbs new file mode 100644 index 0000000000..4aefb9897f --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/Layouts/Full.html.hbs @@ -0,0 +1,140 @@ + + + + + + Bitwarden + + + + + + + + + + + +
+ + + + +
+ + + + +
+ + {{>@partial-block}} + +
+ + + + + +
+
+ + + + + diff --git a/src/Core/MailTemplates/Handlebars/Layouts/Full.text.hbs b/src/Core/MailTemplates/Handlebars/Layouts/Full.text.hbs new file mode 100644 index 0000000000..3618575c3f --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/Layouts/Full.text.hbs @@ -0,0 +1,8 @@ +{{>@partial-block}} + +---------------------------- + +- Twitter: https://twitter.com/bitwarden_app +- Facebook: https://www.facebook.com/bitwarden/ +- Google+: https://plus.google.com/+bitwarden +- GitHub: https://github.com/bitwarden \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/MasterPasswordHint.html.hbs b/src/Core/MailTemplates/Handlebars/MasterPasswordHint.html.hbs new file mode 100644 index 0000000000..5b5b9324ff --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/MasterPasswordHint.html.hbs @@ -0,0 +1,8 @@ +{{#>FullHtmlLayout}} +

You (or someone) recently requested your master password hint.

+

+ Your hint is: "{{Hint}}"
+ Log in: {{{link WebVaultUrl}}} +

+

If you did not request your master password hint you can safely ignore this email.

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/MasterPasswordHint.text.hbs b/src/Core/MailTemplates/Handlebars/MasterPasswordHint.text.hbs new file mode 100644 index 0000000000..7caaa07b9e --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/MasterPasswordHint.text.hbs @@ -0,0 +1,8 @@ +{{#>BasicTextLayout}} +You (or someone) recently requested your master password hint. + +Your hint is: "{{Hint}}" +Log in: {{{WebVaultUrl}}} + +If you did not request your master password hint you can safely ignore this email. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/NoMasterPasswordHint.html.hbs b/src/Core/MailTemplates/Handlebars/NoMasterPasswordHint.html.hbs new file mode 100644 index 0000000000..2fdc105623 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/NoMasterPasswordHint.html.hbs @@ -0,0 +1,7 @@ +{{#>FullHtmlLayout}} +

+ You (or someone) recently requested your master password hint. + Unfortunately, your account does not have a master password hint. +

+

If you did not request your master password hint you can safely ignore this email.

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/NoMasterPasswordHint.text.hbs b/src/Core/MailTemplates/Handlebars/NoMasterPasswordHint.text.hbs new file mode 100644 index 0000000000..ae22830219 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/NoMasterPasswordHint.text.hbs @@ -0,0 +1,5 @@ +{{#>BasicTextLayout}} +You (or someone) recently requested your master password hint. Unfortunately, your account does not have a master password hint. + +If you did not request your master password hint you can safely ignore this email. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/OrganizationUserAccepted.html.hbs b/src/Core/MailTemplates/Handlebars/OrganizationUserAccepted.html.hbs new file mode 100644 index 0000000000..d81c2f1998 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/OrganizationUserAccepted.html.hbs @@ -0,0 +1,5 @@ +{{#>FullHtmlLayout}} +

This email is to notify you that {{UserEmail}} has accepted your invitation to join {{OrganizationName}}.

+

To confirm this user, log into the Bitwarden web vault, manage your organization "People, and confirm the user.

+

If you do not wish to confirm this user, you can also remove them from the organization on the same page.

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/OrganizationUserAccepted.text.hbs b/src/Core/MailTemplates/Handlebars/OrganizationUserAccepted.text.hbs new file mode 100644 index 0000000000..188172cfa7 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/OrganizationUserAccepted.text.hbs @@ -0,0 +1,7 @@ +{{#>BasicTextLayout}} +This email is to notify you that {{UserEmail}} has accepted your invitation to join {{OrganizationName}}. + +To confirm this user, log into the Bitwarden web vault, manage your organization "People" and confirm the user. + +If you do not wish to confirm this user, you can also remove them from the organization on the same page. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/OrganizationUserConfirmed.html.hbs b/src/Core/MailTemplates/Handlebars/OrganizationUserConfirmed.html.hbs new file mode 100644 index 0000000000..29bb36af3a --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/OrganizationUserConfirmed.html.hbs @@ -0,0 +1,4 @@ +{{#>FullHtmlLayout}} +

This email is to notify you that you have been confirmed as a user of {{OrganizationName}}.

+

Any collections and logins being shared with you by this organization will now appear in your Bitwarden vault.

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/OrganizationUserConfirmed.text.hbs b/src/Core/MailTemplates/Handlebars/OrganizationUserConfirmed.text.hbs new file mode 100644 index 0000000000..7a7c1a498e --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/OrganizationUserConfirmed.text.hbs @@ -0,0 +1,5 @@ +{{#>BasicTextLayout}} +This email is to notify you that you have been confirmed as a user of {{OrganizationName}}. + +Any collections and logins being shared with you by this organization will now appear in your Bitwarden vault. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/OrganizationUserInvited.html.hbs b/src/Core/MailTemplates/Handlebars/OrganizationUserInvited.html.hbs new file mode 100644 index 0000000000..0c5beed403 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/OrganizationUserInvited.html.hbs @@ -0,0 +1,11 @@ +{{#>FullHtmlLayout}} +

+ You have been invited to join the {{OrganizationName}} organization. + To accept this invite, click the following link: +

+

{{{link Url 'true'}}}

+

+ If you do not wish to join this organization, you can safely ignore + this email. +

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/OrganizationUserInvited.text.hbs b/src/Core/MailTemplates/Handlebars/OrganizationUserInvited.text.hbs new file mode 100644 index 0000000000..0f806836d1 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/OrganizationUserInvited.text.hbs @@ -0,0 +1,7 @@ +{{#>BasicTextLayout}} +You have been invited to join the {{OrganizationName}} organization. To accept this invite, click the following link: + +{{{Url}}} + +If you do not wish to join this organization, you can safely ignore this email. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/PasswordlessSignIn.html.hbs b/src/Core/MailTemplates/Handlebars/PasswordlessSignIn.html.hbs new file mode 100644 index 0000000000..7bfa7288f7 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/PasswordlessSignIn.html.hbs @@ -0,0 +1,9 @@ +{{#>FullHtmlLayout}} +

+ Click the following link to log in: +

+

{{{link Url 'true'}}}

+

+ If you did not request to log in, you can safely ignore this email. +

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/PasswordlessSignIn.text.hbs b/src/Core/MailTemplates/Handlebars/PasswordlessSignIn.text.hbs new file mode 100644 index 0000000000..e6dea236bd --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/PasswordlessSignIn.text.hbs @@ -0,0 +1,7 @@ +{{#>BasicTextLayout}} +Click the following link to log in: + +{{{Url}}} + +If you did not request to log in, you can safely ignore this email. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/TwoFactorEmail.html.hbs b/src/Core/MailTemplates/Handlebars/TwoFactorEmail.html.hbs new file mode 100644 index 0000000000..113333d055 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/TwoFactorEmail.html.hbs @@ -0,0 +1,8 @@ +{{#>FullHtmlLayout}} +

+ Your two-step verification code is: {{Token}} +

+

+ Use this code to complete logging in with Bitwarden. +

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/TwoFactorEmail.text.hbs b/src/Core/MailTemplates/Handlebars/TwoFactorEmail.text.hbs new file mode 100644 index 0000000000..c7e64e5da2 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/TwoFactorEmail.text.hbs @@ -0,0 +1,5 @@ +{{#>BasicTextLayout}} +Your two-step verification code is: {{Token}} + +Use this code to complete logging in with Bitwarden. +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/VerifyDelete.html.hbs b/src/Core/MailTemplates/Handlebars/VerifyDelete.html.hbs new file mode 100644 index 0000000000..15add90352 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/VerifyDelete.html.hbs @@ -0,0 +1,7 @@ +{{#>FullHtmlLayout}} +

+ Click the link below to delete your Bitwarden account ({{Email}}). + If you did not request this email to delete your Bitwarden account, you can safely ignore it. +

+

{{{link Url 'true'}}}

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/VerifyDelete.text.hbs b/src/Core/MailTemplates/Handlebars/VerifyDelete.text.hbs new file mode 100644 index 0000000000..6d9f508c64 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/VerifyDelete.text.hbs @@ -0,0 +1,5 @@ +{{#>BasicTextLayout}} +Click the link below to delete your Bitwarden account ({{Email}}). If you did not request this email to delete your Bitwarden account, you can safely ignore it. + +{{{Url}}} +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/VerifyEmail.html.hbs b/src/Core/MailTemplates/Handlebars/VerifyEmail.html.hbs new file mode 100644 index 0000000000..9f837f3717 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/VerifyEmail.html.hbs @@ -0,0 +1,7 @@ +{{#>FullHtmlLayout}} +

+ Verify this email address for your Bitwarden account by clicking the following link. + If you did not request this email to verify a Bitwarden account, you can safely ignore it. +

+

{{{link Url 'true'}}}

+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/VerifyEmail.text.hbs b/src/Core/MailTemplates/Handlebars/VerifyEmail.text.hbs new file mode 100644 index 0000000000..3a7fde5305 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/VerifyEmail.text.hbs @@ -0,0 +1,5 @@ +{{#>BasicTextLayout}} +Verify this email address for your Bitwarden account by clicking the following link. If you did not request this email to verify a Bitwarden account, you can safely ignore it. + +{{{Url}}} +{{/BasicTextLayout}} \ No newline at end of file diff --git a/src/Core/MailTemplates/Handlebars/Welcome.html.hbs b/src/Core/MailTemplates/Handlebars/Welcome.html.hbs new file mode 100644 index 0000000000..92b35ac644 --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/Welcome.html.hbs @@ -0,0 +1,93 @@ +{{#>FullHtmlLayout}} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Thank you for creating an account with Bitwarden. You may now log in with your new account. +
+ Did you know that Bitwarden is free to sync with all of your devices? Download Bitwarden today on: +
+ Desktop +
+ Access Bitwarden on Windows, macOS, and Linux desktops with our native desktop application. +
+ + Windows, macOS, and Linux + +
+ Web Browser +
+ Integrate Bitwarden directly into your favorite browser. Use our browser extensions for a seamless browsing experience. +
+ + Chrome, Firefox, Opera, Edge, Safari, and more + +
+ Mobile +
+ Take Bitwarden on the go with our mobile apps for your phone or tablet device. +
+ + App Store and Google Play + +
+ Web +
+ Stuck without any of your devices? Using a friend's computer? You can access your Bitwarden vault from any web enabled device by using the web vault. +
+ If you have any questions or problems you can email us from our website at https://bitwarden.com/contact. +
+ Thank you!
+ The Bitwarden Team +
+{{/FullHtmlLayout}} diff --git a/src/Core/MailTemplates/Handlebars/Welcome.text.hbs b/src/Core/MailTemplates/Handlebars/Welcome.text.hbs new file mode 100644 index 0000000000..536a14241d --- /dev/null +++ b/src/Core/MailTemplates/Handlebars/Welcome.text.hbs @@ -0,0 +1,35 @@ +{{#>FullTextLayout}} +Thank you for creating an account with Bitwarden. You may now log in with your new account. + +Did you know that Bitwarden is free to sync with all of your devices? Download Bitwarden today on: + +Desktop +============ + +Access Bitwarden on Windows, macOS, and Linux desktops with our native desktop application. +https://bitwarden.com/#download + +Web Browser +============ + +Integrate Bitwarden directly into your favorite browser. Use our browser extensions for a seamless browsing experience. +https://bitwarden.com/#download-browser + +Mobile +============ + +Take Bitwarden on the go with our mobile apps for your phone or tablet device. +https://bitwarden.com/#download-mobile + +Web +============ + +Stuck without any of your devices? Using a friend's computer? You can access your Bitwarden vault from any web enabled device by using the web vault. +{{WebVaultUrl}}/?utm_source=welcome_email&utm_medium=email + + +If you have any questions or problems you can email us from our website at: https://bitwarden.com/contact/?utm_source=welcome_email&utm_medium=email + +Thank you! +The Bitwarden Team +{{/FullTextLayout}} \ No newline at end of file diff --git a/src/Core/Services/Implementations/HandlebarsMailService.cs b/src/Core/Services/Implementations/HandlebarsMailService.cs new file mode 100644 index 0000000000..f155b69fc6 --- /dev/null +++ b/src/Core/Services/Implementations/HandlebarsMailService.cs @@ -0,0 +1,376 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Bit.Core.Models.Table; +using Bit.Core.Models.Mail; +using System.IO; +using System.Net; +using Bit.Core.Utilities; +using System.Linq; +using System.Reflection; +using HandlebarsDotNet; + +namespace Bit.Core.Services +{ + public class HandlebarsMailService : IMailService + { + private const string Namespace = "Bit.Core.MailTemplates.Handlebars"; + + private readonly GlobalSettings _globalSettings; + private readonly IMailDeliveryService _mailDeliveryService; + private readonly Dictionary> _templateCache = + new Dictionary>(); + + private bool _registeredHelpersAndPartials = false; + + public HandlebarsMailService( + GlobalSettings globalSettings, + IMailDeliveryService mailDeliveryService) + { + _globalSettings = globalSettings; + _mailDeliveryService = mailDeliveryService; + } + + public async Task SendVerifyEmailEmailAsync(string email, Guid userId, string token) + { + var message = CreateDefaultMessage("Verify Your Email", email); + var model = new VerifyEmailModel + { + Token = WebUtility.UrlEncode(token), + UserId = userId, + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "VerifyEmail", model); + message.MetaData.Add("SendGridBypassListManagement", true); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendVerifyDeleteEmailAsync(string email, Guid userId, string token) + { + var message = CreateDefaultMessage("Delete Your Account", email); + var model = new VerifyDeleteModel + { + Token = WebUtility.UrlEncode(token), + UserId = userId, + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName, + Email = email, + EmailEncoded = WebUtility.UrlEncode(email) + }; + await AddMessageContentAsync(message, "VerifyDelete", model); + message.MetaData.Add("SendGridBypassListManagement", true); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail) + { + var message = CreateDefaultMessage("Your Email Change", toEmail); + var model = new ChangeEmailExistsViewModel + { + FromEmail = fromEmail, + ToEmail = toEmail, + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "ChangeEmailAlreadyExists", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendChangeEmailEmailAsync(string newEmailAddress, string token) + { + var message = CreateDefaultMessage("Your Email Change", newEmailAddress); + var model = new EmailTokenViewModel + { + Token = token, + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "ChangeEmail", model); + message.MetaData.Add("SendGridBypassListManagement", true); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendTwoFactorEmailAsync(string email, string token) + { + var message = CreateDefaultMessage("Your Two-step Login Verification Code", email); + var model = new EmailTokenViewModel + { + Token = token, + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "TwoFactorEmail", model); + message.MetaData.Add("SendGridBypassListManagement", true); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendMasterPasswordHintEmailAsync(string email, string hint) + { + var message = CreateDefaultMessage("Your Master Password Hint", email); + var model = new MasterPasswordHintViewModel + { + Hint = CoreHelpers.SanitizeForEmail(hint), + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "MasterPasswordHint", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendNoMasterPasswordHintEmailAsync(string email) + { + var message = CreateDefaultMessage("Your Master Password Hint", email); + var model = new BaseMailModel + { + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "NoMasterPasswordHint", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendOrganizationAcceptedEmailAsync(string organizationName, string userEmail, + IEnumerable adminEmails) + { + var message = CreateDefaultMessage($"User {userEmail} Has Accepted Invite", adminEmails); + var model = new OrganizationUserAcceptedViewModel + { + OrganizationName = CoreHelpers.SanitizeForEmail(organizationName), + UserEmail = userEmail, + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "OrganizationUserAccepted", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendOrganizationConfirmedEmailAsync(string organizationName, string email) + { + var message = CreateDefaultMessage($"You Have Been Confirmed To {organizationName}", email); + var model = new OrganizationUserConfirmedViewModel + { + OrganizationName = CoreHelpers.SanitizeForEmail(organizationName), + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "OrganizationUserConfirmed", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendOrganizationInviteEmailAsync(string organizationName, OrganizationUser orgUser, string token) + { + var message = CreateDefaultMessage($"Join {organizationName}", orgUser.Email); + var model = new OrganizationUserInvitedViewModel + { + OrganizationName = CoreHelpers.SanitizeForEmail(organizationName), + Email = WebUtility.UrlEncode(orgUser.Email), + OrganizationId = orgUser.OrganizationId.ToString(), + OrganizationUserId = orgUser.Id.ToString(), + Token = WebUtility.UrlEncode(token), + OrganizationNameUrlEncoded = WebUtility.UrlEncode(organizationName), + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "OrganizationUserInvited", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendWelcomeEmailAsync(User user) + { + var message = CreateDefaultMessage("Welcome", user.Email); + var model = new BaseMailModel + { + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName + }; + await AddMessageContentAsync(message, "Welcome", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendPasswordlessSignInAsync(string returnUrl, string token, string email) + { + var message = CreateDefaultMessage("[Admin] Continue Logging In", email); + var url = CoreHelpers.ExtendQuery(new Uri($"{_globalSettings.BaseServiceUri.Admin}/login/confirm"), + new Dictionary + { + ["returnUrl"] = returnUrl, + ["email"] = email, + ["token"] = token, + }); + var model = new PasswordlessSignInModel + { + Url = url.ToString() + }; + await AddMessageContentAsync(message, "PasswordlessSignIn", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + public async Task SendInvoiceUpcomingAsync(string email, decimal amount, DateTime dueDate, + List items, bool mentionInvoices) + { + var message = CreateDefaultMessage("Your Subscription Will Renew Soon", email); + var model = new InvoiceUpcomingViewModel + { + WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, + SiteName = _globalSettings.SiteName, + AmountDue = amount, + DueDate = dueDate, + Items = items, + MentionInvoices = mentionInvoices + }; + await AddMessageContentAsync(message, "InvoiceUpcoming", model); + await _mailDeliveryService.SendEmailAsync(message); + } + + private MailMessage CreateDefaultMessage(string subject, string toEmail) + { + return CreateDefaultMessage(subject, new List { toEmail }); + } + + private MailMessage CreateDefaultMessage(string subject, IEnumerable toEmails) + { + return new MailMessage + { + ToEmails = toEmails, + Subject = subject, + MetaData = new Dictionary() + }; + } + + private async Task AddMessageContentAsync(MailMessage message, string templateName, T model) + { + message.HtmlContent = await RenderAsync($"{templateName}.html", model); + message.TextContent = await RenderAsync($"{templateName}.text", model); + } + + private async Task RenderAsync(string templateName, T model) + { + await RegisterHelpersAndPartialsAsync(); + if(!_templateCache.TryGetValue(templateName, out var template)) + { + var source = await ReadSourceAsync(templateName); + if(source != null) + { + template = Handlebars.Compile(source); + _templateCache.Add(templateName, template); + } + } + return template != null ? template(model) : null; + } + + private async Task ReadSourceAsync(string templateName) + { + var assembly = typeof(HandlebarsMailService).GetTypeInfo().Assembly; + var fullTemplateName = $"{Namespace}.{templateName}.hbs"; + if(!assembly.GetManifestResourceNames().Any(f => f == fullTemplateName)) + { + return null; + } + using(var s = assembly.GetManifestResourceStream(fullTemplateName)) + using(var sr = new StreamReader(s)) + { + return await sr.ReadToEndAsync(); + } + } + + private async Task RegisterHelpersAndPartialsAsync() + { + if(_registeredHelpersAndPartials) + { + return; + } + _registeredHelpersAndPartials = true; + + var basicHtmlLayoutSource = await ReadSourceAsync("Layouts.Basic.html"); + Handlebars.RegisterTemplate("BasicHtmlLayout", basicHtmlLayoutSource); + var basicTextLayoutSource = await ReadSourceAsync("Layouts.Basic.text"); + Handlebars.RegisterTemplate("BasicTextLayout", basicTextLayoutSource); + var fullHtmlLayoutSource = await ReadSourceAsync("Layouts.Full.html"); + Handlebars.RegisterTemplate("FullHtmlLayout", fullHtmlLayoutSource); + var fullTextLayoutSource = await ReadSourceAsync("Layouts.Full.text"); + Handlebars.RegisterTemplate("FullTextLayout", fullTextLayoutSource); + + Handlebars.RegisterHelper("date", (writer, context, parameters) => + { + if(parameters.Length == 0 || !(parameters[0] is DateTime)) + { + writer.WriteSafeString(string.Empty); + return; + } + if(parameters.Length > 0 && parameters[1] is string) + { + writer.WriteSafeString(((DateTime)parameters[0]).ToString(parameters[1].ToString())); + } + else + { + writer.WriteSafeString(((DateTime)parameters[0]).ToString()); + } + }); + + Handlebars.RegisterHelper("usd", (writer, context, parameters) => + { + if(parameters.Length == 0 || !(parameters[0] is decimal)) + { + writer.WriteSafeString(string.Empty); + return; + } + writer.WriteSafeString(((decimal)parameters[0]).ToString("C")); + }); + + Handlebars.RegisterHelper("link", (writer, context, parameters) => + { + if(parameters.Length == 0) + { + writer.WriteSafeString(string.Empty); + return; + } + + var text = parameters[0].ToString(); + var href = text; + var clickTrackingOff = false; + if(parameters.Length == 2) + { + if(parameters[1] is string) + { + var p1 = parameters[1].ToString(); + if(p1 == "true" || p1 == "false") + { + clickTrackingOff = p1 == "true"; + } + else + { + href = p1; + } + } + else if(parameters[1] is bool) + { + clickTrackingOff = (bool)parameters[1]; + } + } + else if(parameters.Length > 2) + { + if(parameters[1] is string) + { + href = parameters[1].ToString(); + } + if(parameters[2] is string) + { + var p2 = parameters[2].ToString(); + if(p2 == "true" || p2 == "false") + { + clickTrackingOff = p2 == "true"; + } + } + else if(parameters[2] is bool) + { + clickTrackingOff = (bool)parameters[2]; + } + } + + var clickTrackingText = (clickTrackingOff ? "clicktracking=off" : string.Empty); + writer.WriteSafeString($"{text}"); + }); + } + } +} diff --git a/src/Core/Utilities/ServiceCollectionExtensions.cs b/src/Core/Utilities/ServiceCollectionExtensions.cs index a2d386631f..09b04e4d54 100644 --- a/src/Core/Utilities/ServiceCollectionExtensions.cs +++ b/src/Core/Utilities/ServiceCollectionExtensions.cs @@ -66,7 +66,7 @@ namespace Bit.Core.Utilities public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings) { - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton();