Send Email Using PowerShell

Send Email Using PowerShell

Windows PowerShell cmdlets have the capability of sending emails. We can craft an email and its various components, such as recipients, email body, email attachments, etc. using PS code. This feature removes manual effort and human intervention 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

 
  1. $runDateTime = Get-Date    
  2. $smtp = "mailservice.redduck.com"    
  3. $to = "[email protected]"     
  4. $cc = "[email protected]"       
  5. $from = "[email protected]"        
  6. $subject = "Script Run Completed - $runDateTime"    
  7. $attachmentLocation = "C:\Users\Jarvis\Desktop\TestFile.txt"     
  8.     
  9. $body = "Hello Team, <br><br>"         
  10. $body += "The program <b>Fantastic Powershell Program</b> has been completed successfully at <i>$runDateTime.</i><br><br>"    
  11. $body += "Have a great day!<br><br>"    
  12. $body += "Thank you"    
  13. Send-MailMessage -SmtpServer $smtp -To $to -Cc $cc -From $from -Subject $subject -Body $body -BodyAsHtml -Attachments $attachmentLocation  
  14.   
  15. 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 – “[email protected]”, “[email protected]
-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 that is responsible for establishing 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.

Send Email Using PowerShell


Similar Articles