When automating tasks in powershell notifications are key, luckily this task is made much simpler with powershells “Send-MailMessage” cmdlet.
In a simplistic form you could use a one liner:
1 |
send-mailmessage -to "$emailTo" -from "$emailFrom" -subject "$subject" -body $message -smtpserver $smtpserver -credential $creds |
But if you plan on calling this often, why not build a simple function to handle all emails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function Mailer ($SO, $ID, $Reason) { $message = @" Warning: $ID with SalesOrder: $SO for reason: $Reason Please confirm that the information is correct. "@ #Set Creds $secpasswd = ConvertTo-SecureString "PASSWORD" -AsPlainText -Force $creds = New-Object System.Management.Automation.PSCredential ("USERNAME", $secpasswd) #Set email vars $emailFrom = "EMAILFROM" $subject="SUBJECT $ID with $SO" $smtpserver="SERVER" $emailTo="EMAILTO" send-mailmessage -to "$emailTo" -from "$emailFrom" -subject "$subject" -body $message -smtpserver $smtpserver -credential $creds } |
The function “Mailer” above will take in 3 parameters and use them to send an email to a pre-configured address. This script could easily be extended to allow it to mail a particular person in the event of a particular error.