Send email using PowerShell
Windows PowerShell cmdlets has the capabilities of sending emails. We can craft email and its various components such as recipients, email body, email attachments, etc. using PS code. This feature removes manual efforts and human interventions of composing and sending emails. This opens various avenues to make your programs more dynamic as well as appealing business-wise.
Use Cases
A few use cases where this capability is instrumental are –
- Send email to target audience at the end of a script, with the result files as attachments
- Send email as a part of monitoring scripts. For instance, a PS script sending out mailers when a SharePoint subsite exceeds a size limit set by administrator
- Send email to administrator when a .NET site is not used frequently, based on site visits thresholds set by administrators
The Script
$runDateTime = Get-Date $smtp = "mailservice.redduck.com" $to = "customer1@redduck.com" $cc = "customer2@redduck.com" $from = "guardbot@redduck.com" $subject = "Script Run Completed - $runDateTime" $attachmentLocation = "C:\Users\Jarvis\Desktop\TestFile.txt" $body = "Hello Team, <br><br>" $body += "The program <b>Fantastic Powershell Program</b> has been completed successfully at <i>$runDateTime.</i><br><br>" $body += "Have a great day!<br><br>" $body += "Thank you" Send-MailMessage -SmtpServer $smtp -To $to -Cc $cc -From $from -Subject $subject -Body $body -BodyAsHtml -Attachments $attachmentLocation Write-Host "====== Email sent! ======"
Explanation
The primary cmdlet responsible for sending the email is Send-MailMessage. It accepts various parameters. Below is a description of the parameters that are primarily used –
Parameters |
Type |
Description/Remarks |
-To | String | The recipient of the email.
For multiple recipients separate them as individual strings. For example – “user1@demo.com”, “user2@demo.com” |
-From | String | The sender of the email |
-Cc | String | The recipient who will be copied in the email |
-Bcc | String | The recipient who will be copied but not listed in the email |
-Subject | String | The subject line of the email body |
-Body | String | The email body of the message.
Multiple lines including variables can also be appended. HTML elements and stylings (bold, italics, etc.) can also be included as shown in the script above. |
-Attachments | String | The path of the attachment file
It is worth noting that, for attachments that exceed the limits set by the email service provider, it might fail. Hence, it is recommended to exercise caution for before attaching files in emails. For larger attachments, shared locations can be used. |
-SmtpServer | String | The SMPT server responsible for establishing the communication. This is very vital. |
-BodyAsHtml | String | This parameter specifies that the email body contains HTML and will be rendered as per HTML standards. |
Illustration
Modify the parameters and variables as per your requirements. Execute the program. And that’s it!
The email will be delivered to your recipients! Here is a glimpse
4,748 total views, 2 views today
0 Comments