Wednesday, January 11, 2012

Using Rich HTML with Send-MailMessage

By:Rik Hoffelder
The Send-MailMessage CMDLET can be a useful tool in many ways. However it has some limitations that can be easily overcome with a little PowerShell scripting and HTML tags. I was recently asked by a customer for some ideas to help automate a notification message when a new employee joins the company. This was to be a part of a larger auto-user provisioning script that would send a welcome message to the new employee with various company information and helpful links. This particular task was performed manually by an administrator using a custom template in Outlook (.OFT file). Our mission was to convert the OFT and manual process into something we could do with Send-MailMessage.

Before I go too much further into the solution I want to thank and credit my customer, Paul Savage of McCarthy Building Companies, for pulling this all together and sharing his code with us. Thank you Paul!

First it is important to understand that Send-MailMessage allows you to use HTML in the message body using the –HtmlAsBody switch. Next we take advantage of the Get-Content and Out-String cmdlets to build the Body parameter of the message. This allows us to create a txt file with HTML tags that contain the message content and provide the same formatting that was used in the body of the OFT. So with the assistance of Paul's Sharepoint Developer we were able to create the message body text file as shown below:



.colorchange {




<html xmlns="">


<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt">Welcome to Company Name Here!&nbsp; We are glad you are here!&nbsp;&nbsp; Below are some helpful items to get you started.</p>

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt">Our company provides a library of documents to help you with your daily tasks and responsibilities <a href=" ">by clicking here.</a>.</p>

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt">The <a href="">Human Resources Policies Manual</a> should be thoroughly reviewed.&nbsp; By replying, you are acknowledging receipt and consent to the Policies outlined in the Manual.</p>

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt">In addition to policies noted above, please take a moment to review and understand a few additional guidelines when using our Information Systems.&nbsp; They are: </p>

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt">&nbsp;1.&nbsp; Use of the Mass Distribution Function Company E-Mail System- <a href="">http://</a><br />

&nbsp;2.&nbsp; Spam E-Mails- <a href="http://">http://</a> <br />

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt">Again, the intent of these policies and procedures are to provide for necessary safeguards that protect all employees interests and our company.&nbsp;&nbsp; If you have any questions on the policies or procedures, feel free to contact the I.T. Help Desk at 800-555-1234.&nbsp; They will forward your questions to the appropriate personnel who can assist you.</p>

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt">Thank you and welcome aboard!</p>

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:10pt"><b>Jane Doe</b> | <span class="colorchange">Director</span></p>

<p style="font-family:Tahoma, Geneva, sans-serif; font-size:8pt; color:#808080">Human Resources | Company Name, Inc.<br/>(800) 555-1234</p>



Next we reference the body file in the script as shown below:


$messageParameters = @{

Subject = "Welcome Aboard!"

Body = Get-Content "C:\body.txt" | out-string

From = ""

To = ""

SmtpServer = ""


Send-MailMessage @messageParameters –BodyAsHtml

With a little bit of tweaking you can easily pass a variable to the script by adding the following:

SendWelcomeEmail.PS1 NewEmp

(Example .\SendWelcomeEmail.PS1


    [string] $NewEmp = ""


function ValidateParams


$validInputs = $true

$errorString = ""

if ($NewEmp -eq "")


    $validInputs = $false

    $errorString += "`missing parameter: The employee email address parameter is required. Please enter a valid email address. Example:"


if (!$validInputs)


    Write-error "$errorString"


return $validInputs


### Validate the parameters ###

$ifValidParams = ValidateParams;

if (!$ifValidParams) { exit; }

$messageParameters = @{

Subject = "Welcome to the Company!"

Body = Get-Content "C:\body.txt" | out-string

From = ""

To = &NewEmp

SmtpServer = ""


Send-MailMessage @messageParameters –BodyAsHtml


Good luck and happy scripting!

More information on PowerShell
blog comments powered by Disqus
Microsoft Virtualization, Citrix, XENServer, Storage, iscsi, Exchange, Virtual Desktops, XENDesktop, APPSense, Netscaler, Virtual Storage, VM, Unified Comminications, Cisco, Server Virtualization, Thin client, Server Based Computing, SBC, Application Delivery controllers, System Center, SCCM, SCVMM, SCOM, VMware, VSphere, Virtual Storage, Cloud Computing, Provisioning Server, Hypervisor, Client Hypervisor.