Send HTML email with PowerShell

Quite a few Exchange administrator like to send HTML email with PowerShell scripts for reporting to a group of people, and I’m no different. The code below is my solution to sending HTML email messages using the CSS style element to control all the formatting.  This includes font family, font size, table borders, and table header background color. The goal of the HTML design decisions here are an attempt to improve readability on smart phones by using a  smaller font size and color in the table headers to add some dimension.

Below is the head section of the HTML message and there a few things to take note of.  You will see at the end of each line there is an `r`n.  These are special characters used for formatting which creates a carriage return and new line.  On line 7 you will see the H3 tag which includes style for any H3 tag used in the HMTL messages.

$HTMLHead = "`r`n"
$HTMLHead += "`r`n"
$HTMLHead += "</pre>`r`n"
$HTMLHead += "TABLE{border: 1px solid black; border-collapse: collapse; font-family: Arial, Helvetica, sans-serif; font-size: 8pt;}`r`n"
$HTMLHead += "TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}`r`n"
$HTMLHead += "TD{border: 1px solid black; padding: 5px;}`r`n"
$HTMLHead += "H3{text-align:left; font-family: Arial, Helvetica, sans-serif; font-size: 15pt;}`r`n"
$HTMLHead += "<pre>`r`n"
$HTMLHead += "`r`n"

When using the ConvertTo-HTML cmdlet and the -fragment tag you are only creating a table. Unfortunately there is no way to control the formatting of the HTML output and it is a bit of a mess. You can find additional information about the ConvertTo-HTML cmdlet on technet.

$HTMLProcesses = $Processes | convertto-html -Fragment @{Label="Process Name";Expression={$_.ProcessName}}, `
@{Label="CPU"; Expression = {$_.CPU}},@{Label="Process ID"; Expression = {$_.ID}}

Finally you’ll want to put together various HTML sections and form the final HTML document that will be sent as a report. Be careful to make sure that your HTML document is well formed. As the complexity of your reports increases it become more likely you’ll make the mistake of adding extra HTML code that will cause some formatting headaches.

$MailMessage= "
$HTMLHead

</pre>
<center>
<h3><b>Table Header</b></h3>
</center>
<pre>
$HTMLProcesses"

The body of the email message will appear as below:
PowerShell HTML table

The example script is below:

<!--

Code highlighting produced by Actipro SyntaxEditor
http://www.ActiproSoftware.com/Products/DotNet/

--><span style="color: #008000;" data-mce-style="color: #008000;">#</span><span style="color: #008000;" data-mce-style="color: #008000;"> Get the currently running processes and only store svchost processes.</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$Processes</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span> <span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">Get-Process</span><span style="color: #000000;" data-mce-style="color: #000000;"> | </span><span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">Where-Object</span><span style="color: #000000;" data-mce-style="color: #000000;">{</span><span style="color: #800080;" data-mce-style="color: #800080;">$_</span><span style="color: #000000;" data-mce-style="color: #000000;">.ProcessName </span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">-eq</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">svchost</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #000000;" data-mce-style="color: #000000;">}

</span><span style="color: #008000;" data-mce-style="color: #008000;">#</span><span style="color: #008000;" data-mce-style="color: #008000;"> Generate the HTML head section and include style elements.</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;"><Html xmlns=`"http://www.w3.org/1999/xhtml`">`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;"><Head>`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;"><Style>`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">TABLE{border: 1px solid black; border-collapse: collapse; font-family: Arial, Helvetica, sans-serif; font-size: 8pt;}`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">TD{border: 1px solid black; padding: 5px;}`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">H3{text-align:left; font-family: Arial, Helvetica, sans-serif; font-size: 15pt;}`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;"></Style>`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLHead</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;"></Head>`r`n</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>

<span style="color: #008000;" data-mce-style="color: #008000;">#</span><span style="color: #008000;" data-mce-style="color: #008000;"> Convert the processes stored in $Processes with the -fragment switch and only</span><span style="color: #008000;" data-mce-style="color: #008000;">
#</span><span style="color: #008000;" data-mce-style="color: #008000;">  select a few key parts of the output.</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$HTMLProcesses</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span> <span style="color: #800080;" data-mce-style="color: #800080;">$Processes</span><span style="color: #000000;" data-mce-style="color: #000000;"> | </span><span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">convertto-html</span> <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-Fragment</span><span style="color: #000000;" data-mce-style="color: #000000;"> @{Label</span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">Process Name</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #000000;" data-mce-style="color: #000000;">;Expression</span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span><span style="color: #000000;" data-mce-style="color: #000000;">{</span><span style="color: #800080;" data-mce-style="color: #800080;">$_</span><span style="color: #000000;" data-mce-style="color: #000000;">.ProcessName}}, </span><span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">`</span><span style="color: #000000;" data-mce-style="color: #000000;">
    @{Label</span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">CPU</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #000000;" data-mce-style="color: #000000;">; Expression </span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span><span style="color: #000000;" data-mce-style="color: #000000;"> {</span><span style="color: #800080;" data-mce-style="color: #800080;">$_</span><span style="color: #000000;" data-mce-style="color: #000000;">.CPU}},@{Label</span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">Process ID</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #000000;" data-mce-style="color: #000000;">; Expression </span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span><span style="color: #000000;" data-mce-style="color: #000000;"> {</span><span style="color: #800080;" data-mce-style="color: #800080;">$_</span><span style="color: #000000;" data-mce-style="color: #000000;">.ID}}

</span><span style="color: #008000;" data-mce-style="color: #008000;">#</span><span style="color: #008000;" data-mce-style="color: #008000;"> Create HTML email body by adding all the head section and $HTMLProcess table from above.</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$MailMessage</span><span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">
$HTMLHead
 <body>
     <center>
        <h3><b>Table Header</b></h3>
    </center>
    $HTMLProcesses
 </body>
</html>
</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>

<span style="color: #008000;" data-mce-style="color: #008000;">#</span><span style="color: #008000;" data-mce-style="color: #008000;"> Get the date and time.</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$DateTime</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span> <span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">Get-Date</span> <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-Format</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">ddd MM/dd/yyyy h:mm tt</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
<span style="color: #008000;" data-mce-style="color: #008000;">#</span><span style="color: #008000;" data-mce-style="color: #008000;"> Set subject line to include the $DateTime variable.</span>
<span style="color: #800080;" data-mce-style="color: #800080;">$EmailSubject</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">Send HTML email with PowerShell</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">+</span> <span style="color: #800080;" data-mce-style="color: #800080;">$DateTime</span>

<span style="color: #008000;" data-mce-style="color: #008000;">#</span><span style="color: #008000;" data-mce-style="color: #008000;"> Send an email with all the compiled data.</span>
<span style="color: #000000;" data-mce-style="color: #000000;">[</span><span style="color: #008080;" data-mce-style="color: #008080;">string</span><span style="color: #000000;" data-mce-style="color: #000000;">[]] </span><span style="color: #800080;" data-mce-style="color: #800080;">$EmailTo</span> <span style="color: #ff0000;" data-mce-style="color: #ff0000;">=</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">babcockscott@get-mailbox.net</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>
  <span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">Send-MailMessage</span> <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-To</span> <span style="color: #800080;" data-mce-style="color: #800080;">$EmailTo</span> <span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">`</span>
  <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-Subject</span> <span style="color: #800080;" data-mce-style="color: #800080;">$EmailSubject</span> <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-From</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">Reporting@get-mailbox.net</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span> <span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">`</span>
  <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-Body</span> <span style="color: #800080;" data-mce-style="color: #800080;">$MailMessage</span> <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-BodyasHTML</span> <span style="color: #5f9ea0; font-weight: bold;" data-mce-style="color: #5f9ea0; font-weight: bold;">`</span>
  <span style="color: #5f9ea0; font-style: italic;" data-mce-style="color: #5f9ea0; font-style: italic;">-SmtpServer</span> <span style="color: #800000;" data-mce-style="color: #800000;">"</span><span style="color: #800000;" data-mce-style="color: #800000;">MailRelayServer</span><span style="color: #800000;" data-mce-style="color: #800000;">"</span>

 

1 thought on “Send HTML email with PowerShell”

Leave a Comment