Files

39 lines
1.2 KiB
PowerShell

param(
[Parameter(Mandatory = $true)]
[string]$To
)
$envFile = Join-Path $PSScriptRoot "..\.env"
if (-not (Test-Path $envFile)) {
throw "Missing .env. Copy .env.example to .env and enter the SMTP settings."
}
Get-Content $envFile | ForEach-Object {
if ($_ -match "^\s*([^#=\s]+)\s*=\s*(.*)\s*$") {
[Environment]::SetEnvironmentVariable($matches[1], $matches[2], "Process")
}
}
$required = "MAIL_HOST", "MAIL_PORT", "MAIL_USERNAME", "MAIL_PASSWORD", "MAIL_FROM"
$missing = $required | Where-Object { [string]::IsNullOrWhiteSpace([Environment]::GetEnvironmentVariable($_, "Process")) }
if ($missing) {
throw "Missing required SMTP settings: $($missing -join ', ')"
}
$message = [System.Net.Mail.MailMessage]::new($env:MAIL_FROM, $To)
$message.Subject = "Eternity Project SMTP verification"
$message.Body = "SMTP delivery verification completed at $(Get-Date -Format o)."
$client = [System.Net.Mail.SmtpClient]::new($env:MAIL_HOST, [int]$env:MAIL_PORT)
$client.EnableSsl = $true
$client.Credentials = [System.Net.NetworkCredential]::new($env:MAIL_USERNAME, $env:MAIL_PASSWORD)
try {
$client.Send($message)
Write-Output "SMTP_TEST_SENT_TO=$To"
}
finally {
$message.Dispose()
$client.Dispose()
}