# Based on code by tysonkopczynski (http://poshcode.org/1035)
# Added Inline attachments by Simon Henderson
#-------------------------------------------------
# Send-HTMLFormattedEmail
#-------------------------------------------------
# Usage:	Send-HTMLFormattedEmail -?
#-------------------------------------------------
function Send-HTMLFormattedEmail {
	<# 
	.Synopsis
    	Used to send an HTML Formatted Email.
    .Description
    	Used to send an HTML Formatted Email that is based on an XSLT template.
	.Parameter To
		Email address or addresses for whom the message is being sent to.
		Addresses should be seperated using ;.
	.Parameter ToDisName
		Display name for whom the message is being sent to.
	.Parameter CC
		Email address if you want CC a recipient.
		Addresses should be seperated using ;.
	.Parameter BCC
		Email address if you want BCC a recipient.
		Addresses should be seperated using ;.
	.Parameter From
		Email address for whom the message comes from.
	.Parameter FromDisName
		Display name for whom the message comes from.
	.Parameter Subject
		The subject of the email address.
	.Parameter Content
		The content of the message (to be inserted into the XSL Template).
	.Parameter Relay
		FQDN or IP of the SMTP relay to send the message to.
	.XSLPath
		The full path to the XSL template that is to be used.
	#>
    param(
		[Parameter(Mandatory=$True)][String]$To,
		[Parameter(Mandatory=$True)][String]$ToDisName,
		[String]$CC,
		[String]$BCC,
		[Parameter(Mandatory=$True)][String]$From,
		[Parameter(Mandatory=$True)][String]$FromDisName,
		[Parameter(Mandatory=$True)][String]$Subject,
		[Parameter(Mandatory=$True)][String]$Content,
		[Parameter(Mandatory=$True)][String]$Relay,
		[Parameter(Mandatory=$True)][String]$XSLPath
		
        )
    
    try {
		
		#logo used in signature
		$Logopic = "c:\\scripts\\logo.png"
		
        $Message = New-Object System.Net.Mail.MailMessage
		
		# add the attachment, and set it to inline.
		$Attachment = New-Object Net.Mail.Attachment("c:\\scripts\\logo.png")
		$Attachment.ContentDisposition.Inline = $True
		$Attachment.ContentDisposition.DispositionType = "Inline"
		$Attachment.ContentType.MediaType = "image/jpg"
		$Logo = "cid:logo" 
				
        # Load XSL Argument List
        $XSLArg = New-Object System.Xml.Xsl.XsltArgumentList
        $XSLArg.Clear() 
        $XSLArg.AddParam("To", $Null, $ToDisName)
        $XSLArg.AddParam("Content", $Null, $Content)
		$XSLArg.AddParam("Logo", $Null, $Logo)

		
        # 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()
		

		
		# Populate the Message.
		$html = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString($body, $null, "text/html")
		$imageToSend = new-object system.net.mail.linkedresource($Logopic,"image/jpg")
		$imageToSend.ContentID = "logo"
		$html.LinkedResources.Add($imageToSend)
		$message.AlternateViews.Add($html)
		
        $Message.Subject = $Subject
        $Message.IsBodyHTML = $True
		
		
		# Add From
        $MessFrom = New-Object System.Net.Mail.MailAddress $From, $FromDisName
		$Message.From = $MessFrom

		# Add To
		$To = $To.Split(";") # Make an array of addresses.
		$To | foreach {$Message.To.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
		
		# Add CC
		if ($CC){
			$CC = $CC.Split(";") # Make an array of addresses.
			$CC | foreach {$Message.CC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
			}

		# Add BCC
		if ($BCC){
			$BCC = $BCC.Split(";") # Make an array of addresses.
			$BCC | foreach {$Message.BCC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
			}
     
        # Create SMTP Client
        $Client = New-Object System.Net.Mail.SmtpClient $Relay

        # Send The Message
        $Client.Send($Message)
        }  
    catch {
		throw $_
        }   
		$attachment.Dispose() #dispose or it'll lock the file
    }

	
#################################################################################
#####																		#####
#####  	CUT AND PASTE EVERYTHING BELOW HERE INTO A NEW .XSL FILE			#####
#####	THIS IS THE FILE REFERENCED TO BY THE XSLPATH PARAMETER				#####
#####																		#####
#################################################################################
	
	
	
<?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:param name="Logo"/>

	
<xsl:attribute-set name="image-style">
  <xsl:attribute name="style">float:left; margin-right:0px; margin-bottom:0px</xsl:attribute>
  <xsl:attribute name="alt">green</xsl:attribute>
  <xsl:attribute name="src"><xsl:value-of select="$Logo" /></xsl:attribute>
  <xsl:attribute name="title">SP logo</xsl:attribute>
</xsl:attribute-set>

	
    <xsl:template match="/">
        <html>
            <head>
                <title>Your Password is due to Expire!</title>
            </head>
            <body>
            <div width="400px">
                <p>Hi <xsl:value-of select="$To" />,</p>
                <p></p>
                <p>Your windows password is due to expire in <xsl:value-of select="$Content" /> days.</p>
                <p></p>
				<p>This password is used to access your email, logon to work PCs, connect to the vpn, and various intranet sites.</p>
				<p></p>
				<p>If you are working on an Office computer, please press CTRL-ALT-DELETE and choose change password. Follow the instructions to set your new password. </p>
				<p></p>
				<p>If you are on a Sales laptop, please visit <a href="https://webmail.mydomain.com/">https://webmail.mydomain.com</a>, login and follow the prompts.</p>
				<p></p>
				<ul>
					<li>Must contain upper and lower case letters</li>
					<li>Must contain a number or special character (like !@#$%^)</li>
					<li>Must not be the same as your previous passwords</li>
					<li>Must not be similar to your username (if any three letters appear in the same order, it will be rejected)</li>
				</ul>
				<p><strong>Failure to change your password will likely result in your inability to perform your job correctly.</strong><br /></p>
				<p>If any point you have any questions or concerns please open a help desk ticket by replying to this email, or contact IT on (08) 5555 5555</p>
				<p></p>
            <Address>
			Many thanks,<br />	
            IT Team<br />
			</Address>
			<xsl:element name="img" use-attribute-sets="image-style"></xsl:element>
		</div>
      </body>
    </html>
    </xsl:template> 
</xsl:stylesheet>