I wrote a script that will forward messages to as many recipients as you'd like. I have only done basic testing, so there may be issues. Please thoroughly test it before implementing in a production environment.
This script will not forward to a combination of local and remote recipients. It will do one or the other.
The script allows the original message to be delivered to the mailbox.
The script is searching and replacing headers in the email message. If there are strings that match the headers in any part of the message, including the message body, they will be replaced. For example, if the body has "X-Return-Path:" in it, it will be replaced with "X-Return-Path: <$OriginalRecipient>" It does not decode message content, if the body of the message is encoded it will not match.
param(
[string]$MSGFileName
)
################################################################################################################################
################################################################################################################################
## Forward message to lots of people v 1.0.0 ##
## May 14 2025 ##
## Copyright MDaemon Technologies 2025 ##
## ##
## This script is designed to be ran by the content filter in MDaemon. It accepts the path to a message file as a parameter. ##
## The script creates a copy of the message for each recipient in the $Recipients variable and places it in the remote queue. ##
## ##
## You can forward to all local users by changing the $RemoteQ variable to point to the local queue. This script is not ##
## designed to forward to a combination of local and remote recipients. ##
## ##
## This script may cause issues if messages that it is processing contain emails as attachment or email headers in the body ##
## of the messages. ##
## ##
## Here is a sample content filter rule that can be used to call the script: ##
## ##
## RuleName=Forward messages sent to user@localdomain.com ##
## Enable=Yes ##
## ThisRuleCondition=All ##
## ProcessQueue=LOCAL ##
## Condition01=X-MDAEMON-DELIVER-TO|contains|AND|user@localdomain.com| ##
## Action01=run a program|"-1,0,0","C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass ##
## c:\mdaemon\PowerShell\ForwardtoLotsofEmails.ps1 -MSGFileName "$MESSAGEFILENAME$"" ##
## ##
## ##
## ##
## MDaemon Technologies offers no warranty, provides no support, and is not responsible for any damages that may be arise ##
## from the use of this script. Use at your own risk. ##
## ##
################################################################################################################################
################################################################################################################################
#Set these variables to the correct values!!
$Recipients = ("user1@externaldomain.com","user2@otherexternaldomain.com")
$OriginalRecipient = "user@localdomain.com"
$RemoteQ = "c:\mdaemon\queues\Remote\"
$FileName = Split-Path $MSGFileName -leaf
$AttachmentFileNameWOExtension = [System.IO.Path]::GetFileNameWithoutExtension($FileName)
$AttachmentFileNameExtension = [System.IO.Path]::GetExtension($FileName)
[bigint]$FileNumber = $AttachmentFileNameWOExtension -replace "[^0-9]" , ""
$RemoteQueueFilePath = Join-Path $RemoteQ $FileName
$newLines = @("X-MDRedirect: 1",
"X-MDRedirect_From: $OriginalRecipient"
)
$existingContent = Get-Content $MSGFileName
$combinedContent = $newLines + $existingContent
ForEach ($Recipient in $Recipients){
$combinedContent = $combinedContent -replace "X-MDaemon-Deliver-To: $OriginalRecipient", "X-MDaemon-Deliver-To: $Recipient"
$combinedContent = $combinedContent -replace "X-Return-Path:.*", "X-Return-Path: <$OriginalRecipient>"
While(Test-Path $RemoteQueueFilePath){
$FileNumber += 1
$NewFileName = "pd" + $FileNumber + $AttachmentFileNameExtension
$RemoteQueueFilePath = Join-Path $RemoteQ $NewFileName
}
Write-Host "RemoteQ Path: $RemoteQueueFilePath"
Set-Content $combinedContent -Path $RemoteQueueFilePath
}