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:
and the output:
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 to multiple recipients:
It Just needs an array!
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 <[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;"> <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:
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
No comments:
Please Login and comment to get your questions answered!