Problem Description
Sometimes you need a Powershell Script to solve Problems quick and dirty.
In my case some of the incoming Mails in my SMTP Server got automaticly deleted after a missmatch in SenderID and SPF Record. (Sender ID Policy in my Exchange Edge 2016 Infrastructure) in cause of that, i must inform the Sender that there Emails never reached me. I used a generic Server Information Message.
Get-MessageTrackingLog (Source File)
I exported the log of all deleted Mails with Timestamp, Sender, Recipients, MessageSubject. You can find the command here
Get-MessageTrackingLog -Server MBX01 -Start "07/20/2017 09:00:00" -End "07/20/2018 17:00:00" -ResultSize Unlimited -EventID FAIL
Having some Script Fun
- First of all copy the comma delimited file into a source path like „C:/Script/“
- Change the Variables in my Script
- Take sure the Column names in your CSV matches with the Variables Names in the Script
- Execute the Script from a authenticated Server (SMTP Server) or implement a „-credetial“ with username and password for each loop.
#Variables (Source Email Adress)
$EmailFrom = "Postmaster <postmaster@drkomtur.com>"
#Your SMTP Server
$EmailSMTPserver = "YourSMTPServer"
# if needed choose onFailure or OnSuccess or nothing
$EmailDeliveryNotificationOption = "onFailure, onSuccess"
# SourceFile use your own FilePath to the Source File.csv
$SourcePath = "C:\Scripts\yourcsvfile.csv"
# Import SourceFile comma-seperated
$Users = Import-Csv -Path $SourcePath
# Loop for each
foreach ($User in $Users) {
# User's email address
$EmailTo = $User.Sender
# Subject for each Email
$EmailSubject = " Message delivery failed to " + $User.Recipients + " send at " + $User.Timestamp + " Subject: " + $User.MessageSubject + "."
# Email body with HTML formatting
$EmailBody = "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">"
$EmailBody += "<html xmlns=""http://www.w3.org/1999/xhtml""><head>"
$EmailBody += "<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />"
$EmailBody += "<meta name=""viewport"" content=""width=device-width, initial-scale=1.0""/>"
$EmailBody += "<title>" + $EmailSubject + "</title>"
$EmailBody += "</head><body bgcolor=""#FFFFFF"" style=""font-family: sans-serif; color: #000000"">"
$EmailBody += "<p><strong>Failure notification message for:</strong></p>"
$EmailBody += "<p><strong>Fehler bei der Nachrichtenzustellung an folgende Empfaenger oder Gruppen:</strong></p>"
$EmailBody += "<p></p>"
$EmailBody += "<p></p>"
$EmailBody += "</head><body bgcolor=""#FFFFFF"" style=""font-family: tahoma; color: #A4A4A4"">"
$EmailBody += "<p>Server: Your SMTP Server</p>"
$EmailBody += "<p>Remote Server returned SOCKET NULL; not found'</p>"
$EmailBody += "<p></p>"
$EmailBody += "<p></p>"
$EmailBody += "</head><body bgcolor=""#FFFFFF"" style=""font-family: courier-new; color: #BDBDBD"">"
$EmailBody += "<p><ul><li>This is a delivery failure notification message indicating that your email <strong>" + $User.MessageSubject + " </strong>you addressed to email address : <strong>" + $User.Recipients + " </strong>could not be delivered.</li>"
$EmailBody += "<li>The problem appears to be :<strong> -- Communications error Additional information follows : -- Null result from socket This condition occurred after 30 attempt(s) to deliver over a period of 24 hour(s).</strong></li>"
$EmailBody += "<li>Additional Info: If you sent the email to multiple recipients, you will receive one of these messages for <strong>each</strong> one which failed delivery, otherwise they have been sent.</li></ul></p>"
$EmailBody += "<p></p>"
$EmailBody += "<p><strong>Please be advised, you should send this Email again</strong></p>"
$EmailBody += "<p></p>"
$EmailBody += "</body></html>"
# Merge and sending emails
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -BodyAsHTML -DeliveryNotificationOption $EmailDeliveryNotificationOption -SmtpServer $EmailSmtpServer -Port 25
}
# End
As you can see in the script the script will add automaticly add the Subject, Timestamp, Reciepeint and sender informations automaticly to the Subject and body of your Mail.
Just try it with a small bunch of users first and take sure not to send too much mails in a short period of time to avoid to get blacklisted from a public Provider.
You need assistance or got some questions? Just send me a mail via my Contact Form

Let's go and write a comment