File: D:/bin/scripts/MailServerQueueCheck.ps1
# (SS,17/01/2020) send alert email if emails in hMailServer queue reach a threshold of 100
# It does this by checking the number of files in the D:\hMailServer\Data folder
# Alert email is sent via the backup mailserver (mail.ontheworldweb.com) to a Gmail address
# Local server isn't used due to possible delay caused by large queue size
$filepath = "D:\hMailServer\Data"
$filetype = "*.eml"
$file_count = [System.IO.Directory]::GetFiles("$filepath", "$filetype").Count
Write-Output "Emails in queue: $file_count"
Add-Content -Path 'D:\logs\hMailServer Events\MailServerQueueCheck.log' -Value "$(Get-Date -Format "dd/MM/yyyy HH:mm:ss"), $file_count"
If ($file_count -ge 100) {
$subject = 'Mail Server Queue Alert (' + $file_count + ')'
$body = 'hMailServer queue has reached ' + $file_count
$SMTPServer = 'mail.ontheworldweb.com'
Send-MailMessage -From 'Mail Server Queue Check <hmailserver@itpartnership.com>' -To 'Surinder <srinda@gmail.com>' -Subject $subject -Priority High -Body $body -SmtpServer $SMTPServer
}