mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-23 06:35:13 +01:00
* Store Emails * fix test * Update email rules layout * Cleanups * Test cleanups * Add back comments * Update view; add test * Show email rules link even if email settings aren't completed * Validate email addresses * No redirect, display warning * Fix test * Refactoring: Change email argument types to MailAddress * Test fix * Refactoring: Use MailboxAddress * Parse emails properly in controllers and backend Co-authored-by: Dennis Reimann <mail@dennisreimann.de> Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
47 lines
1.7 KiB
C#
47 lines
1.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
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 mail = emailSettings.CreateMailMessage(email, cc, bcc, subject, message, true);
|
|
await smtp.SendAsync(mail, cancellationToken);
|
|
await smtp.DisconnectAsync(true, cancellationToken);
|
|
}, TimeSpan.Zero);
|
|
}
|
|
|
|
public abstract Task<EmailSettings> GetEmailSettings();
|
|
}
|
|
}
|