**Updated** Sending an HTML Formatted Email Using PowerShell

So, I have been working on building a PowerShell based notification engine for a favorite MSFT product of mine called Certificate Lifecycle Manager (kidding!).  Anyhow, I wanted to make the emails pretty (after all, everyone loves pretty emails).  To do this, I figured the best method was send out HTML formatted emails.  However, I did not want to have the email layout necessarily embedded within my PowerShell code.

Employing Google, I searched around.  Sadly, at first I really didn't see anything that peaked my interested.  Almost ready to write the code myself, I then found a really great blog posting by Erik McCarty which had some code that did almost exactly what I wanted (Hurray!).

Considering that I like sharing, below is the function that came about from that posting and the relevant XSLT example.  Additionally, I posted this on PoshCode.

**Updated**

I made a number of improvements to the function that I previously posted.  The function is now a module which has help, can accept an address list, better error handling, etc.  This module can be found on PoshCode.  To use this module, just import the module into your PowerShell session using the Import-Module cmdlet (use PowerShell v2.0 CTP3 or greater).

Once you have the module imported, you can make use of the Send-HTMLFormattedEmail cmdlet.  For example:

Send-HTMLFormattedEmail -To bob@aol.com -ToDisName "Bob Barker" -From thepope@vaticancity.va -FromDisName "The Pope" -Subject "Golf Time!" -Content "I really want to play this Sunday, are you up for 18 holes?" -Relay smtp.popemail.com -XSLPath "C:\templates\holymailtemplate.xsl"

Again, the format for the XSL template should be as follows:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

<xsl:output media-type="xml" omit-xml-declaration="yes" />

    <xsl:param name="To"/>

    <xsl:param name="Content"/>

    <xsl:template match="/">

        <html>

            <head>

                <title>Pope Mail</title>

            </head>

            <body>

            <div width="400px">

                <p>Dear <xsl:value-of select="$To" />,</p>

                <p></p>

                <p><xsl:value-of select="$Content" /></p>

                <p></p>

            <Address>

            Many thanks from your:<br />     

            Holiness<br />

            </Address>

        </div>

      </body>

    </html>

    </xsl:template>

</xsl:stylesheet>

If you like this, check out some other posts from Tyson:

Or if you want, you can also check out some of Tyson's latest publications:

Lastly, visit the Microsoft Subnet for more news, blogs, and opinions from around the Internet.  Or, sign up for the bi-weekly Microsoft newsletter.  (Click on News/Microsoft News Alert)

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2009 IDG Communications, Inc.

IT Salary Survey: The results are in