Format ./web/backend/Infrastructure/Infrastructure.Email

This commit is contained in:
Aaron Po
2026-06-20 15:13:01 -04:00
parent c8bddae817
commit 2f28ac9350
3 changed files with 14 additions and 37 deletions

View File

@@ -21,10 +21,5 @@ public interface IEmailProvider
/// <param name="subject">Email subject line</param> /// <param name="subject">Email subject line</param>
/// <param name="body">Email body (HTML or plain text)</param> /// <param name="body">Email body (HTML or plain text)</param>
/// <param name="isHtml">Whether the body is HTML (default: true)</param> /// <param name="isHtml">Whether the body is HTML (default: true)</param>
Task SendAsync( Task SendAsync(IEnumerable<string> to, string subject, string body, bool isHtml = true);
IEnumerable<string> to,
string subject,
string body,
bool isHtml = true
);
} }

View File

@@ -58,22 +58,16 @@ public class SmtpEmailProvider : IEmailProvider
{ {
_host = _host =
Environment.GetEnvironmentVariable("SMTP_HOST") Environment.GetEnvironmentVariable("SMTP_HOST")
?? throw new InvalidOperationException( ?? throw new InvalidOperationException("SMTP_HOST environment variable is not set");
"SMTP_HOST environment variable is not set"
);
string portString = string portString = Environment.GetEnvironmentVariable("SMTP_PORT") ?? "587";
Environment.GetEnvironmentVariable("SMTP_PORT") ?? "587";
if (!int.TryParse(portString, out _port)) if (!int.TryParse(portString, out _port))
throw new InvalidOperationException( throw new InvalidOperationException($"SMTP_PORT '{portString}' is not a valid integer");
$"SMTP_PORT '{portString}' is not a valid integer"
);
_username = Environment.GetEnvironmentVariable("SMTP_USERNAME"); _username = Environment.GetEnvironmentVariable("SMTP_USERNAME");
_password = Environment.GetEnvironmentVariable("SMTP_PASSWORD"); _password = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
string useSslString = string useSslString = Environment.GetEnvironmentVariable("SMTP_USE_SSL") ?? "true";
Environment.GetEnvironmentVariable("SMTP_USE_SSL") ?? "true";
_useSsl = bool.Parse(useSslString); _useSsl = bool.Parse(useSslString);
_fromEmail = _fromEmail =
@@ -82,9 +76,7 @@ public class SmtpEmailProvider : IEmailProvider
"SMTP_FROM_EMAIL environment variable is not set" "SMTP_FROM_EMAIL environment variable is not set"
); );
_fromName = _fromName = Environment.GetEnvironmentVariable("SMTP_FROM_NAME") ?? "The Biergarten";
Environment.GetEnvironmentVariable("SMTP_FROM_NAME")
?? "The Biergarten";
} }
/// <summary> /// <summary>
@@ -95,12 +87,7 @@ public class SmtpEmailProvider : IEmailProvider
/// <param name="body">Email body (HTML or plain text)</param> /// <param name="body">Email body (HTML or plain text)</param>
/// <param name="isHtml">Whether the body is HTML (default: true)</param> /// <param name="isHtml">Whether the body is HTML (default: true)</param>
/// <exception cref="InvalidOperationException">Thrown when connecting, authenticating, or sending via SMTP fails.</exception> /// <exception cref="InvalidOperationException">Thrown when connecting, authenticating, or sending via SMTP fails.</exception>
public async Task SendAsync( public async Task SendAsync(string to, string subject, string body, bool isHtml = true)
string to,
string subject,
string body,
bool isHtml = true
)
{ {
await SendAsync([to], subject, body, isHtml); await SendAsync([to], subject, body, isHtml);
} }
@@ -128,7 +115,8 @@ public class SmtpEmailProvider : IEmailProvider
MimeMessage message = new(); MimeMessage message = new();
message.From.Add(new MailboxAddress(_fromName, _fromEmail)); message.From.Add(new MailboxAddress(_fromName, _fromEmail));
foreach (string recipient in to) message.To.Add(MailboxAddress.Parse(recipient)); foreach (string recipient in to)
message.To.Add(MailboxAddress.Parse(recipient));
message.Subject = subject; message.Subject = subject;
@@ -152,10 +140,7 @@ public class SmtpEmailProvider : IEmailProvider
await client.ConnectAsync(_host, _port, secureSocketOptions); await client.ConnectAsync(_host, _port, secureSocketOptions);
// Authenticate if credentials are provided // Authenticate if credentials are provided
if ( if (!string.IsNullOrEmpty(_username) && !string.IsNullOrEmpty(_password))
!string.IsNullOrEmpty(_username)
&& !string.IsNullOrEmpty(_password)
)
await client.AuthenticateAsync(_username, _password); await client.AuthenticateAsync(_username, _password);
await client.SendAsync(message); await client.SendAsync(message);
@@ -163,10 +148,7 @@ public class SmtpEmailProvider : IEmailProvider
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new InvalidOperationException( throw new InvalidOperationException($"Failed to send email: {ex.Message}", ex);
$"Failed to send email: {ex.Message}",
ex
);
} }
} }
} }