using System.Collections.Generic; using BTCPayServer.Abstractions.Models; using BTCPayServer.Plugins.Emails.HostedServices; using BTCPayServer.Plugins.Emails.Views; using BTCPayServer.Plugins.Webhooks; using BTCPayServer.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace BTCPayServer.Plugins.Emails; public class EmailsPlugin : BaseBTCPayServerPlugin { public const string Area = "Emails"; public override string Identifier => "BTCPayServer.Plugins.Emails"; public override string Name => "Emails"; public override string Description => "Allows you to send emails to your customers!"; public override void Execute(IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); RegisterServerEmailTriggers(services); } private static string BODY_STYLE = "font-family: Open Sans, Helvetica Neue,Arial,sans-serif; font-color: #292929;"; private static string HEADER_HTML = "

{Branding.ServerName}


"; private static string BUTTON_HTML = "{button_description}"; private static string CallToAction(string actionName, string actionLink) { var button = $"{BUTTON_HTML}".Replace("{button_description}", actionName, System.StringComparison.InvariantCulture); return button.Replace("{button_link}", actionLink, System.StringComparison.InvariantCulture); } private static string CreateEmailBody(string body) => $"{HEADER_HTML}{body}"; private void RegisterServerEmailTriggers(IServiceCollection services) { List vms = new(); var vm = new EmailTriggerViewModel() { Trigger = ServerMailTriggers.PasswordReset, DefaultEmail = new() { To = ["{User.MailboxAddress}"], Subject = "Update Password", Body = CreateEmailBody($"A request has been made to reset your {{Branding.ServerName}} password. Please set your password by clicking below.

{CallToAction("Update Password", "{ResetLink}")}"), }, PlaceHolders = new() { new ("{ResetLink}", "The link to the password reset page") }, Description = "User: Password Reset Requested", }; vms.Add(vm); vm = new EmailTriggerViewModel() { Trigger = ServerMailTriggers.EmailConfirm, DefaultEmail = new() { To = ["{User.MailboxAddress}"], Subject = "Confirm your email address", Body = CreateEmailBody($"Please confirm your account.

{CallToAction("Confirm Email", "{ConfirmLink}")}"), }, PlaceHolders = new() { new ("{ConfirmLink}", "The link used to confirm the user's email address") }, Description = "User: Email confirmation", }; vms.Add(vm); vm = new EmailTriggerViewModel() { Trigger = ServerMailTriggers.InvitePending, DefaultEmail = new() { To = ["{User.MailboxAddress}"], Subject = "Invitation to join {Branding.ServerName}", Body = CreateEmailBody($"

Please complete your account setup by clicking this link.

You can also use the BTCPay Server app and scan this QR code when connecting:

{{InvitationLinkQR}}"), }, PlaceHolders = new() { new ("{InvitationLink}", "The link where the invited user can set up their account"), new ("{InvitationLinkQR}", "The QR code representation of the invitation link") }, Description = "User: Invitation", }; vms.Add(vm); vm = new EmailTriggerViewModel() { Trigger = ServerMailTriggers.ApprovalConfirmed, DefaultEmail = new() { To = ["{User.MailboxAddress}"], Subject = "Your account has been approved", Body = CreateEmailBody($"Your account has been approved and you can now.

{CallToAction("Login here", "{LoginLink}")}"), }, PlaceHolders = new() { new ("{LoginLink}", "The link that the user can use to login"), }, Description = "User: Account approved", }; vms.Add(vm); vm = new EmailTriggerViewModel() { Trigger = ServerMailTriggers.ApprovalRequest, DefaultEmail = new() { To = ["{Admin.MailboxAddresses}"], Subject = "Approval request to access the server for {User.Email}", Body = CreateEmailBody($"A new user ({{User.MailboxAddress}}), is awaiting approval to access the server.

{CallToAction("Approve", "{ApprovalLink}")}"), }, PlaceHolders = new() { new ("{ApprovalLink}", "The link that the admin needs to use to approve the user"), }, Description = "Admin: Approval request", }; vms.Add(vm); var commonPlaceholders = new List() { new("{Admins.MailboxAddresses}", "The email addresses of the admins separated by a comma"), new("{User.Name}", "The name of the user (eg. John Doe)"), new("{User.Email}", "The email of the user (eg. john.doe@example.com)"), new("{User.MailboxAddress}", "The formatted mailbox address to use when sending an email. (eg. \"John Doe\" )"), new("{Branding.ServerName}", "The name of the server (You can configure this in Server Settings ➡ Branding)"), new("{Branding.ContactUrl}", "The contact URL of the server (You can configure this in Server Settings ➡ Branding)"), }; foreach (var v in vms) { v.ServerTrigger = true; v.PlaceHolders.InsertRange(0, commonPlaceholders); services.AddSingleton(v); } } }