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?

Do you need to send an HTML formatted email from PowerShell? If so, you can use the Send-MailMessge cmdlet with the -BodyAsHtml parameter. I will show you how to use the Send-MailMessage cmdlet to send HTML formatted e-mails. I will also provide some PowerShell examples of using HTML formatting in your e-mails.

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

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

#Configuration Variables for E-mail
$SmtpServer = "smtp.crescent.com" #or IP Address such as "10.125.150.250"
$EmailFrom = "Site Delete Script <SiteDeleteScript@Crescent.com>"
$EmailTo = "SharePointSupport@Crescent.com.com"
$EmailSubject = "Site Delete Script - Daily Report on: "+$ReportDate

#HTML Template
$EmailBody = @"
<table style="width: 68%" style="border-collapse: collapse; border: 1px solid #008080;">
 <tr>
    <td colspan="2" bgcolor="#008080" style="color: #FFFFFF; font-size: large; height: 35px;"> 
        Site Delete Script - Daily Report on VarReportDate  
    </td>
 </tr>
 <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;">
    <b>VarApproved</b></td>
 </tr>
  <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;">
  <b>VarRejected</b></td>
 </tr>
</table>
"@

#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 the 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 = "marcm@crescent.com", "victor <victor@crescent.com>"

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

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “Send HTML Format E-Mails with Send-MailMessage in PowerShell

  • I stored the html code block in an external file and retrieved the HTML template with the Get-Content. The Email was shoot using Send-MailMessage using smtp server as localhost (Papercut) and -BodyAsHtml. The formatting of the email content was lost. Unfortunately, no table/ background color/padding can be seen. Can anyone please guide me ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *