No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

74 líneas
3.4KB

  1. param(
  2. [Parameter(Mandatory = $true)][int]$CampaignId,
  3. [string]$ConfigPath = (Join-Path (Split-Path -Parent $PSScriptRoot) 'config.ini'),
  4. [switch]$Send,
  5. [string]$PickupDirectory
  6. )
  7. $ErrorActionPreference = 'Stop'
  8. . (Join-Path $PSScriptRoot 'dispatch-common.ps1')
  9. if ($CampaignId -lt 1) { throw 'CampaignId must be positive' }
  10. if ([bool]$Send -eq [bool]$PickupDirectory) { throw 'Choose exactly one mode: -Send or -PickupDirectory' }
  11. $context = Get-MailContext $ConfigPath
  12. $settings = $context.Settings
  13. if ($settings['SMTP_TLS'] -ne 'on' -or $settings['SMTP_STARTTLS'] -ne 'on') {
  14. throw 'SMTP_TLS and SMTP_STARTTLS must both be on'
  15. }
  16. $port = 0
  17. if (-not [int]::TryParse($settings['SMTP_PORT'], [ref]$port) -or $port -lt 1 -or $port -gt 65535) {
  18. throw 'Invalid SMTP_PORT'
  19. }
  20. $testTo = [string]$settings['SMTP_TEST_TO']
  21. $parsed = New-Object System.Net.Mail.MailAddress($testTo)
  22. if ($parsed.Address -ne $testTo) { throw 'SMTP_TEST_TO must be a single bare email address' }
  23. $db = Open-MailDatabase $context
  24. $client = $null
  25. $message = $null
  26. $password = $null
  27. try {
  28. $campaign = Get-ApprovedCampaignSnapshot $db $CampaignId
  29. $template = Get-TemplateSnapshot $db $campaign.TemplateId
  30. $html = Add-CampaignFooter $template.Document @{
  31. From = $campaign.From; ReplyTo = $campaign.ReplyTo; Postal = $campaign.Postal }
  32. $message = New-Object System.Net.Mail.MailMessage
  33. $message.From = New-Object System.Net.Mail.MailAddress($campaign.From)
  34. $null = $message.ReplyToList.Add($campaign.ReplyTo)
  35. $null = $message.To.Add($testTo)
  36. $message.Subject = '[TEST] ' + $campaign.Subject
  37. $message.SubjectEncoding = [System.Text.Encoding]::UTF8
  38. $view = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString(
  39. $html, [System.Text.Encoding]::UTF8, 'text/html')
  40. foreach ($image in $template.Images) {
  41. $stream = New-Object System.IO.MemoryStream -ArgumentList (,([byte[]]$image.Bytes))
  42. $resource = New-Object System.Net.Mail.LinkedResource($stream, $image.Type)
  43. $resource.ContentId = $image.Cid
  44. $resource.TransferEncoding = [System.Net.Mime.TransferEncoding]::Base64
  45. $null = $view.LinkedResources.Add($resource)
  46. }
  47. $null = $message.AlternateViews.Add($view)
  48. $client = New-Object System.Net.Mail.SmtpClient($settings['SMTP_HOST'], $port)
  49. $client.Timeout = 20000
  50. if ($Send) {
  51. $password = Load-SmtpPassword $context
  52. $client.UseDefaultCredentials = $false
  53. $client.Credentials = New-Object System.Net.NetworkCredential -ArgumentList @($settings['SMTP_USER'], $password)
  54. $client.EnableSsl = $true
  55. }
  56. else {
  57. if (-not (Test-Path -LiteralPath $PickupDirectory -PathType Container)) {
  58. throw 'Pickup directory does not exist'
  59. }
  60. $client.DeliveryMethod = [System.Net.Mail.SmtpDeliveryMethod]::SpecifiedPickupDirectory
  61. $client.PickupDirectoryLocation = [System.IO.Path]::GetFullPath($PickupDirectory)
  62. }
  63. $client.Send($message)
  64. Write-CampaignAudit $db 'approved_campaign_test_accepted' ([string]$CampaignId) ("Internal test to $testTo")
  65. Write-Output "Approved campaign $CampaignId test accepted for $testTo. Live delivery counts did not advance."
  66. if ($Send) { $sentNote = Save-SentCopySafely $settings $password $message; if ($sentNote) { Write-Output $sentNote } }
  67. }
  68. finally {
  69. if ($client) { $client.Dispose() }
  70. if ($message) { $message.Dispose() }
  71. if ($password) { $password.Dispose() }
  72. $db.Dispose()
  73. }

Powered by TurnKey Linux.