Wednesday, June 10, 2015

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

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

Solution: 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 = "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">&nbsp; 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">&nbsp; 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 Get-Content cmdlet in the script.

Tail: 
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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...