Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

86 lignes
3.7KB

  1. param(
  2. [string]$ConfigPath = (Join-Path (Split-Path -Parent $PSScriptRoot) 'config.ini'),
  3. [string]$PickupDirectory,
  4. [switch]$Send
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. . (Join-Path $PSScriptRoot 'mail-common.ps1')
  8. if ([bool]$Send -eq [bool]$PickupDirectory) {
  9. throw 'Choose exactly one mode: -Send or -PickupDirectory'
  10. }
  11. $context = Get-MailContext -ConfigPath $ConfigPath
  12. $settings = $context.Settings
  13. foreach ($name in @('SMTP_HOST', 'SMTP_PORT', 'SMTP_USER', 'SMTP_FROM', 'SMTP_REPLY_TO', 'SMTP_TEST_TO')) {
  14. if (-not $settings.ContainsKey($name) -or -not $settings[$name]) { throw "Missing setting: $name" }
  15. }
  16. if ($settings['SMTP_TLS'] -ne 'on' -or $settings['SMTP_STARTTLS'] -ne 'on') {
  17. throw 'SMTP_TLS and SMTP_STARTTLS must both be on'
  18. }
  19. $port = 0
  20. if (-not [int]::TryParse($settings['SMTP_PORT'], [ref]$port) -or $port -lt 1 -or $port -gt 65535) {
  21. throw 'Invalid SMTP_PORT'
  22. }
  23. $testTo = New-Object System.Net.Mail.MailAddress($settings['SMTP_TEST_TO'])
  24. if ($testTo.Address -ne $settings['SMTP_TEST_TO']) { throw 'SMTP_TEST_TO must be a single bare email address' }
  25. $client = $null
  26. $message = $null
  27. $password = $null
  28. $imageStream = $null
  29. try {
  30. Add-Type -AssemblyName System.Drawing
  31. $imageStream = New-Object System.IO.MemoryStream
  32. $bitmap = New-Object System.Drawing.Bitmap(1, 1)
  33. try {
  34. $bitmap.SetPixel(0, 0, [System.Drawing.Color]::DarkBlue)
  35. $bitmap.Save($imageStream, [System.Drawing.Imaging.ImageFormat]::Png)
  36. }
  37. finally { $bitmap.Dispose() }
  38. $imageStream.Position = 0
  39. $message = New-Object System.Net.Mail.MailMessage
  40. $message.From = New-Object System.Net.Mail.MailAddress($settings['SMTP_FROM'])
  41. $null = $message.ReplyToList.Add($settings['SMTP_REPLY_TO'])
  42. $null = $message.To.Add($testTo)
  43. $message.Subject = 'MassMailer internal SMTP transport test'
  44. $message.SubjectEncoding = [System.Text.Encoding]::UTF8
  45. $html = '<html><body><p>Internal transport test. No marketing campaign was sent.</p>' +
  46. '<img src="cid:massmailer-test-image" width="1" height="1" alt="Test image"></body></html>'
  47. $view = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString(
  48. $html, [System.Text.Encoding]::UTF8, 'text/html')
  49. $image = New-Object System.Net.Mail.LinkedResource($imageStream, 'image/png')
  50. $image.ContentId = 'massmailer-test-image'
  51. $image.TransferEncoding = [System.Net.Mime.TransferEncoding]::Base64
  52. $null = $view.LinkedResources.Add($image)
  53. $null = $message.AlternateViews.Add($view)
  54. $client = New-Object System.Net.Mail.SmtpClient($settings['SMTP_HOST'], $port)
  55. $client.Timeout = 20000
  56. if ($Send) {
  57. $password = Load-SmtpPassword -Context $context
  58. $client.UseDefaultCredentials = $false
  59. $client.Credentials = New-Object System.Net.NetworkCredential -ArgumentList @($settings['SMTP_USER'], $password)
  60. $client.EnableSsl = $true
  61. $client.Send($message)
  62. Write-Output "Internal SMTP test accepted for $($testTo.Address). Check that inbox and the received MIME."
  63. }
  64. else {
  65. $pickupPath = [System.IO.Path]::GetFullPath($PickupDirectory)
  66. if (-not (Test-Path -LiteralPath $pickupPath -PathType Container)) {
  67. throw "Pickup directory not found: $pickupPath"
  68. }
  69. $client.DeliveryMethod = [System.Net.Mail.SmtpDeliveryMethod]::SpecifiedPickupDirectory
  70. $client.PickupDirectoryLocation = $pickupPath
  71. $client.Send($message)
  72. Write-Output "Test MIME written to pickup directory: $pickupPath"
  73. }
  74. }
  75. finally {
  76. if ($client) { $client.Dispose() }
  77. if ($message) { $message.Dispose() }
  78. if ($imageStream) { $imageStream.Dispose() }
  79. if ($password) { $password.Dispose() }
  80. }

Powered by TurnKey Linux.