PoshCode Archive  Artifact [f60f8b5b46]

Artifact f60f8b5b460c0897c56e53a544583ccba90228dc41d5dfc86b8b4dda03fd0d0a:

  • File Send-HTMLFormattedEmail.ps1 — part of check-in [9ec3076cbb] at 2018-06-10 14:27:20 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: 3835)

# 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: 951
# x-derived-from-id: 1035
# x-archived: 2016-10-09T05:41:14
# x-published: 2009-03-15T04:29: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>