PoshCode Archive  Artifact [7b9cd11617]

Artifact 7b9cd11617f231991c20d82cb9bf49060fc4e39ea91ef8f5db2b2a4be6190359:

  • File Send-HTMLFormattedEmail.ps1 — part of check-in [822e04136c] at 2018-06-10 13:22:15 on branch trunk — Use this function to send an HTML Formatted Email that is based on an XSLT template. This function is based on a blog post by Erik McCarty (http://ewmccarty.spaces.live.com). (user: tysonkopczynski size: 3809)

# encoding: ascii
# api: powershell
# title: Send-HTMLFormattedEmail
# description: Use this function to send an HTML Formatted Email that is based on an XSLT template.  This function is based on a blog post by Erik McCarty (http://ewmccarty.spaces.live.com).
# version: 1.0
# type: function
# author: tysonkopczynski
# license: CC0
# function: Send-HTMLFormattedEmail
# x-poshcode-id: 3213
# x-archived: 2012-04-15T10:45:45
# x-published: 2012-02-08T18:18:00
#
#
#-------------------------------------------------
# Send-HTMLFormattedEmail
#-------------------------------------------------
# Usage:	Used to send an HTML Formatted Email that is based on an XSLT template.
#-------------------------------------------------
function Send-HTMLFormattedEmail{
    param ( [string] $To,
            [string] $ToDisName,
            [string] $BCC,
            [string] $From,
            [string] $FromDisName,
            [string] $Subject,
            [string] $Content,
            [string] $Relay,
            [string] $XSLPath)
    
    try {
        # Load XSL Argument List
        $XSLArg = New-Object System.Xml.Xsl.XsltArgumentList
        $XSLArg.Clear() 
        $XSLArg.AddParam("To", $Null, $ToDisName)
        $XSLArg.AddParam("Content", $Null, $Content)

        # Load Documents
        $BaseXMLDoc = New-Object System.Xml.XmlDocument
        $BaseXMLDoc.LoadXml("<root/>")

        $XSLTrans = New-Object System.Xml.Xsl.XslCompiledTransform
        $XSLTrans.Load($XSLPath)

        #Perform XSL Transform
        $FinalXMLDoc = New-Object System.Xml.XmlDocument
        $MemStream = New-Object System.IO.MemoryStream
     
        $XMLWriter = [System.Xml.XmlWriter]::Create($MemStream)
        $XSLTrans.Transform($BaseXMLDoc, $XSLArg, $XMLWriter)

        $XMLWriter.Flush()
        $MemStream.Position = 0
     
        # Load the results
        $FinalXMLDoc.Load($MemStream) 
        $Body = $FinalXMLDoc.Get_OuterXML()

        # Create from, To, BCC and the message strucures
        $MessFrom = New-Object System.Net.Mail.MailAddress $From, $FromDisName
        $MessTo = New-Object System.Net.Mail.Mailaddress $To
        $MessBCC = New-Object System.Net.Mail.Mailaddress $BCC
        $Message = New-Object System.Net.Mail.MailMessage $MessFrom, $MessTo
        
        # Populate message
        $Message.Subject = $Subject
        $Message.Body    = $Body
        $Message.IsBodyHTML = $True

        # Add BCC
        $Message.BCC.Add($MessBCC)
     
        # Create SMTP Client
        $Client = New-Object System.Net.Mail.SmtpClient $Relay

        # Send The Message
        $Client.Send($Message)
        }  
    catch {
	return $Error[0]
        }   
    }

### XSLT Template Example
<?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>My First Formatted Email</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>
				<p><strong>Please do not respond to this email!</strong><br />
					An automated system sent this email, if any point you have any questions or concerns please open a help desk ticket.</p>
				<p></p>
            <Address>
			Many thanks from your:<br />	
            Really Cool IT Team<br />
            </Address>
        </div>
      </body>
    </html>
    </xsl:template> 
</xsl:stylesheet>