param( [string]$ConfigPath = (Join-Path (Split-Path -Parent $PSScriptRoot) 'config.ini'), [string]$PickupDirectory, [switch]$Send ) $ErrorActionPreference = 'Stop' . (Join-Path $PSScriptRoot 'mail-common.ps1') if ([bool]$Send -eq [bool]$PickupDirectory) { throw 'Choose exactly one mode: -Send or -PickupDirectory' } $context = Get-MailContext -ConfigPath $ConfigPath $settings = $context.Settings foreach ($name in @('SMTP_HOST', 'SMTP_PORT', 'SMTP_USER', 'SMTP_FROM', 'SMTP_REPLY_TO', 'SMTP_TEST_TO')) { if (-not $settings.ContainsKey($name) -or -not $settings[$name]) { throw "Missing setting: $name" } } if ($settings['SMTP_TLS'] -ne 'on' -or $settings['SMTP_STARTTLS'] -ne 'on') { throw 'SMTP_TLS and SMTP_STARTTLS must both be on' } $port = 0 if (-not [int]::TryParse($settings['SMTP_PORT'], [ref]$port) -or $port -lt 1 -or $port -gt 65535) { throw 'Invalid SMTP_PORT' } $testTo = New-Object System.Net.Mail.MailAddress($settings['SMTP_TEST_TO']) if ($testTo.Address -ne $settings['SMTP_TEST_TO']) { throw 'SMTP_TEST_TO must be a single bare email address' } $client = $null $message = $null $password = $null $imageStream = $null try { Add-Type -AssemblyName System.Drawing $imageStream = New-Object System.IO.MemoryStream $bitmap = New-Object System.Drawing.Bitmap(1, 1) try { $bitmap.SetPixel(0, 0, [System.Drawing.Color]::DarkBlue) $bitmap.Save($imageStream, [System.Drawing.Imaging.ImageFormat]::Png) } finally { $bitmap.Dispose() } $imageStream.Position = 0 $message = New-Object System.Net.Mail.MailMessage $message.From = New-Object System.Net.Mail.MailAddress($settings['SMTP_FROM']) $null = $message.ReplyToList.Add($settings['SMTP_REPLY_TO']) $null = $message.To.Add($testTo) $message.Subject = 'MassMailer internal SMTP transport test' $message.SubjectEncoding = [System.Text.Encoding]::UTF8 $html = '

Internal transport test. No marketing campaign was sent.

' + 'Test image' $view = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString( $html, [System.Text.Encoding]::UTF8, 'text/html') $image = New-Object System.Net.Mail.LinkedResource($imageStream, 'image/png') $image.ContentId = 'massmailer-test-image' $image.TransferEncoding = [System.Net.Mime.TransferEncoding]::Base64 $null = $view.LinkedResources.Add($image) $null = $message.AlternateViews.Add($view) $client = New-Object System.Net.Mail.SmtpClient($settings['SMTP_HOST'], $port) $client.Timeout = 20000 if ($Send) { $password = Load-SmtpPassword -Context $context $client.UseDefaultCredentials = $false $client.Credentials = New-Object System.Net.NetworkCredential -ArgumentList @($settings['SMTP_USER'], $password) $client.EnableSsl = $true $client.Send($message) Write-Output "Internal SMTP test accepted for $($testTo.Address). Check that inbox and the received MIME." } else { $pickupPath = [System.IO.Path]::GetFullPath($PickupDirectory) if (-not (Test-Path -LiteralPath $pickupPath -PathType Container)) { throw "Pickup directory not found: $pickupPath" } $client.DeliveryMethod = [System.Net.Mail.SmtpDeliveryMethod]::SpecifiedPickupDirectory $client.PickupDirectoryLocation = $pickupPath $client.Send($message) Write-Output "Test MIME written to pickup directory: $pickupPath" } } finally { if ($client) { $client.Dispose() } if ($message) { $message.Dispose() } if ($imageStream) { $imageStream.Dispose() } if ($password) { $password.Dispose() } }