Send HTML Format E-Mails with Send-MailMessage in PowerShell

Requirement: Send E-mail from PowerShell script with HTML formatted body.

How to use Send-MailMessage with HTML body?

Here is an example for sending E-mail as HTML from PowerShell script with Send-MailMessge cmdlet:

#Get Date
$ReportDate = Get-Date -format "MM-dd-yyyy"

#Configuration Variables for E-mail
$SmtpServer = "" #or IP Address such as ""
$EmailFrom = "Site Delete Script <[email protected]>"
$EmailTo = "[email protected]"
$EmailSubject = "Site Delete Script - Daily Report on: "+$ReportDate

#HTML Template
$EmailBody = @"

<table style="width: 68%" style="border-collapse: collapse; border: 1px solid #008080;">
    <td colspan="2" bgcolor="#008080" style="color: #FFFFFF; font-size: large; height: 35px;"> 
        Site Delete Script - Daily Report on VarReportDate  
 <tr style="border-bottom-style: solid; border-bottom-width: 1px; padding-bottom: 1px">
    <td style="width: 201px; height: 35px">  Number of requests Approved</td>
    <td style="text-align: center; height: 35px; width: 233px;">
  <tr style="height: 39px; border: 1px solid #008080">
  <td style="width: 201px; height: 39px">  Number of requests Rejected</td>
 <td style="text-align: center; height: 39px; width: 233px;">

#Get Values for Approved & Rejected variables
$ApprovedCount= Get-ApprovedReq()
$RejectedCount= Get-RejectedReq()

#Replace the Variables VarApproved, VarRejected and VarReportDate
$EmailBody= $EmailBody.Replace("VarApproved",$ApprovedCount)
$EmailBody= $EmailBody.Replace("VarRejected",$RejectedCount)
$EmailBody= $EmailBody.Replace("VarReportDate",$ReportDate)
#Send E-mail from PowerShell script
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -BodyAsHtml -SmtpServer $SmtpServer

and the output:

powershell send mailmessage body as html

Instead of directly embedding the HTML into the script, you can also store it in an external file and retrieve the HTML format/template with Get-Content cmdlet in the script.

Send-MailMessage in PowerShell with attachments:

Just specify the -attachments parameter in Send-Mailmessage cmdlet. Here is an example:

#send-mailmessage in powershell with attachment
$attachment = "D:\Reports\SiteDeleteRpt.csv"
#send-mailmessage in powershell multiple attachments
$attachment = "D:\Reports\SiteDeleteRpt1.csv","D:\Reports\SiteDeleteRpt2.csv"
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -BodyAsHTML -Attachments $attachment -SmtpServer $SmtpServer 

Send-mailmessage in PowerShell to multiple recipients:

It Just needs an array!

$EmailTo = "[email protected]", "victor <[email protected]>"
#or use:
$recipients = @("Marc M <[email protected]>", "victor <[email protected]>")
How to Get Outgoing E-mail Server? Use this code:
$SPGlobalAdmin = New-Object Microsoft.SharePoint.Administration.SPGlobalAdmin
$SMTPServer = $SPGlobalAdmin.OutboundSmtpServer

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply