Coldfusion 2016: Move ColdFusion Undelivery Mail Back In Spool

You may experienced that ColdFusion (all versions) from time to times does not deliver all mails and moves some mails to Mail\Undelivr folder. These mails stay there and will never delivered. Normally this behavior is correct for emails with invalid email addresses, but in many many cases this happens only because ColdFusion temporarily cannot reach your mail gateway. The root cause here is ColdFusion that does not retry delivery properly like any other properly working email system.

We have no choice here and need to solve the administrative part for not getting the disk filled and retry possible temporarily mail gateway outages automatically. Therefore I have written a scheduled job that runs every 3 hours and moves the mails back to spool folder for about 4 days. After these 4 days of continued retries the *.cfmail files are moved to the Mail\Failed folder and kept there for about 6 months. After 6 months these mails will be deleted from disk. This gives your developers enough time to review these mails and identify their faulty code. I strongly suggest to instruct your developers to validate email addresses properly in all places where tags are used, but you may be out of luck if you provide hosting services to your customers.

Save below PowerShell code as C:\PSScripts\MoveColdFusionUndeliveryMailBackInSpool.ps1:

Set-StrictMode -Version Latest
 
# ColdFusion installation directory
$cfBaseDir = 'C:\ColdFusion2016'
 
if ((Test-Path -path $cfBaseDir)) {
 
    Get-ChildItem $cfBaseDir -attributes d | ForEach-Object {
        $cfInstance = $_.name
 
        $cfMailUndelivr = "$cfBaseDir\$cfInstance\Mail\Undelivr"
        $cfMailSpool = "$cfBaseDir\$cfInstance\Mail\Undelivr"
        $cfMailFailed = "$cfBaseDir\$cfInstance\Mail\Failed"
 
        if ((Test-Path -path $cfMailUndelivr)) {
            if (-Not (Test-Path -path $cfMailFailed)) {
                New-Item -ItemType directory -Path $cfMailFailed -Force
                Write-Host "Directory $cfMailFailed created."
            }
 
            Write-Host "Outdated mails moved to $cfMailFailed"
            Get-ChildItem -Path $cfMailUndelivr -Force -File -ErrorAction SilentlyContinue | Where-Object { $_.CreationTime -lt ((Get-Date).AddDays(-4)) } | Move-Item -Destination $cfMailFailed
 
            Write-Host "Retry mails moved to $cfMailSpool"
            Move-Item "$cfMailUndelivr\*.cfmail" $cfMailSpool
 
            Write-Host "Remove failed mails after 6 months from $cfMailFailed"
            Get-ChildItem -Path $cfMailFailed -Force -File -ErrorAction SilentlyContinue | Where-Object { $_.CreationTime -lt ((Get-Date).AddMonths(-6)) } | Remove-Item -Force -ErrorAction SilentlyContinue
        }
    }
} else {
    Write-Host "$cfBaseDir folder not found."
}

We don't like to run this script manually... so create a job that runs every 3 hours.

$JobName = 'Move ColdFusion Undelivery Mail Back In Spool'
Register-ScheduledJob -Name $JobName -FilePath 'C:\PSScripts\MoveColdFusionUndeliveryMailBackInSpool.ps1'
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '01:00 AM') -Name $JobName
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '04:00 AM') -Name $JobName
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '07:00 AM') -Name $JobName
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '10:00 AM') -Name $JobName
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '01:00 PM') -Name $JobName
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '04:00 PM') -Name $JobName
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '07:00 PM') -Name $JobName
Add-JobTrigger -Trigger (New-JobTrigger -Daily -At '10:00 PM') -Name $JobName

If you may like to delete this task:

Unregister-ScheduledJob -Name 'Move ColdFusion Undelivery Mail Back In Spool' 
Rating
Average: 9.1 (10 votes)