btcpayserver/BTCPayServer/Services/Mails/EmailSender.cs
d11n 09dbe44bca
Onboarding: Invite new users on store level (#5719)
* Onboarding: Invite new users

- Separates the user self-registration and invite cases
- Adds invitation email for users created by the admin
- Adds invitation tokens to verify user was invited
- Adds handler action for invite links
- Refactors `UserEventHostedService`
- Fixes #5726.

* Add permissioned form tag helper

* Better way of changing a user's role

* Test fixes
2024-03-19 14:58:33 +01:00

48 lines
1.8 KiB
C#

using System;
using System.Threading.Tasks;
using BTCPayServer.Logging;
using Microsoft.Extensions.Logging;
using MimeKit;
namespace BTCPayServer.Services.Mails
{
public abstract class EmailSender : IEmailSender
{
public Logs Logs { get; }
readonly IBackgroundJobClient _JobClient;
public EmailSender(IBackgroundJobClient jobClient, Logs logs)
{
Logs = logs;
_JobClient = jobClient ?? throw new ArgumentNullException(nameof(jobClient));
}
public void SendEmail(MailboxAddress email, string subject, string message)
{
SendEmail(new[] { email }, Array.Empty<MailboxAddress>(), Array.Empty<MailboxAddress>(), subject, message);
}
public void SendEmail(MailboxAddress[] email, MailboxAddress[] cc, MailboxAddress[] bcc, string subject, string message)
{
_JobClient.Schedule(async cancellationToken =>
{
var emailSettings = await GetEmailSettings();
if (emailSettings?.IsComplete() != true)
{
Logs.Configuration.LogWarning("Should have sent email, but email settings are not configured");
return;
}
using var smtp = await emailSettings.CreateSmtpClient();
var prefixedSubject = await GetPrefixedSubject(subject);
var mail = emailSettings.CreateMailMessage(email, cc, bcc, prefixedSubject, message, true);
await smtp.SendAsync(mail, cancellationToken);
await smtp.DisconnectAsync(true, cancellationToken);
}, TimeSpan.Zero);
}
public abstract Task<EmailSettings> GetEmailSettings();
public abstract Task<string> GetPrefixedSubject(string subject);
}
}