PoshCode Logo PowerShell Code Repository

Difference between
modified post 3347 by rubrub on Thu 12th Apr 04:42 and
original post 3346 by ddadasf on Thu 12th Apr 04:41
Showold version | new version | both versions

    
11
##################################################
22
# cmdlets
33
##################################################
44
#-------------------------------------------------
55
# Send-HTMLFormattedEmail
66
#-------------------------------------------------
77
# Usage:	Send-HTMLFormattedEmail -?
88
#-------------------------------------------------
99
function Send-HTMLFormattedEmail {
1010
	<#
1111
	.Synopsis
1212
    	Used to send an HTML Formatted Email.
1313
    .Description
1414
    	Used to send an HTML Formatted Email that is based on an XSLT template.
1515
	.Parameter To
1616
		Email address or addresses for whom the message is being sent to.
1717
		Addresses should be seperated using ;.
1818
	.Parameter ToDisName
1919
		Display name for whom the message is being sent to.
2020
	.Parameter CC
2121
		Email address if you want CC a recipient.
2222
		Addresses should be seperated using ;.
2323
	.Parameter BCC
2424
		Email address if you want BCC a recipient.
2525
		Addresses should be seperated using ;.
2626
	.Parameter From
2727
		Email address for whom the message comes from.
2828
	.Parameter FromDisName
2929
		Display name for whom the message comes from.
3030
	.Parameter Subject
3131
		The subject of the email address.
3232
	.Parameter Content
3333
		The content of the message (to be inserted into the XSL Template).
3434
	.Parameter Relay
3535
		FQDN or IP of the SMTP relay to send the message to.
3636
	.XSLPath
3737
		The full path to the XSL template that is to be used.
3838
	#>
3939
    param(
4040
		[Parameter(Mandatory=$True)][String]$To,
4141
		[Parameter(Mandatory=$True)][String]$ToDisName,
4242
		[String]$CC,
4343
		[String]$BCC,
4444
		[Parameter(Mandatory=$True)][String]$From,
4545
		[Parameter(Mandatory=$True)][String]$FromDisName,
4646
		[Parameter(Mandatory=$True)][String]$Subject,
4747
		[Parameter(Mandatory=$True)][String]$Content,
4848
		[Parameter(Mandatory=$True)][String]$Relay,
4949
		[Parameter(Mandatory=$True)][String]$XSLPath
5050
        )
5252
    try {
5353
        # Load XSL Argument List
5454
        $XSLArg = New-Object System.Xml.Xsl.XsltArgumentList
5555
        $XSLArg.Clear()
5656
        $XSLArg.AddParam("To", $Null, $ToDisName)
5757
        $XSLArg.AddParam("Content", $Null, $Content)
5959
        # Load Documents
6060
        $BaseXMLDoc = New-Object System.Xml.XmlDocument
6161
        $BaseXMLDoc.LoadXml("<root/>")
6363
        $XSLTrans = New-Object System.Xml.Xsl.XslCompiledTransform
6464
        $XSLTrans.Load($XSLPath)
6666
        #Perform XSL Transform
6767
        $FinalXMLDoc = New-Object System.Xml.XmlDocument
6868
        $MemStream = New-Object System.IO.MemoryStream
7070
        $XMLWriter = [System.Xml.XmlWriter]::Create($MemStream)
7171
        $XSLTrans.Transform($BaseXMLDoc, $XSLArg, $XMLWriter)
7373
        $XMLWriter.Flush()
7474
        $MemStream.Position = 0
7676
        # Load the results
7777
        $FinalXMLDoc.Load($MemStream)
7878
        $Body = $FinalXMLDoc.Get_OuterXML()
8080
		# Create Message Object
8181
        $Message = New-Object System.Net.Mail.MailMessage
8383
		# Now Populate the Message Object.
8484
        $Message.Subject = $Subject
8585
        $Message.Body = $Body
8686
        $Message.IsBodyHTML = $True
8888
		# Add From
8989
        $MessFrom = New-Object System.Net.Mail.MailAddress $From, $FromDisName
9090
		$Message.From = $MessFrom
9292
		# Add To
9393
		$To = $To.Split(";") # Make an array of addresses.
9494
		$To | foreach {$Message.To.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
9696
		# Add CC
9797
		if ($CC){
9898
			$CC = $CC.Split(";") # Make an array of addresses.
9999
			$CC | foreach {$Message.CC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
100100
			}
102102
		# Add BCC
103103
		if ($BCC){
104104
			$BCC = $BCC.Split(";") # Make an array of addresses.
105105
			$BCC | foreach {$Message.BCC.Add((New-Object System.Net.Mail.Mailaddress $_.Trim()))} # Add them to the message object.
106106
			}
108108
        # Create SMTP Client
109109
        $Client = New-Object System.Net.Mail.SmtpClient $Relay
111111
        # Send The Message
112112
        $Client.Send($Message)
113113
        }
114114
    catch {
115115
		throw $_
116116
        }
117117
    }
119119
##################################################
120120
# Main
121121
##################################################
122122
Export-ModuleMember Send-HTMLFormattedEmail
124124
### XSLT Template Example
125125
<?xml version="1.0"?>
126126
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
128128
<xsl:output media-type="xml" omit-xml-declaration="yes" />
129129
    <xsl:param name="To"/>
130130
    <xsl:param name="Content"/>
131131
    <xsl:template match="/">
132132
        <html>
133133
            <head>
134134
                <title>My First Formatted Email</title>
135135
            </head>
136136
            <body>
137137
            <div width="400px">
138138
                <p>Dear <xsl:value-of select="$To" />,</p>
139139
                <p></p>
140140
                <p><xsl:value-of select="$Content" /></p>
141141
                <p></p>
142142
				<p><strong>Please do not respond to this email!</strong><br />
143143
					An automated system sent this email, if any point you have any questions or concerns please open a help desk ticket.</p>
144144
				<p></p>
145145
            <Address>
146146
			Many thanks from your:<br />
147147
            Really Cool IT Team<br />
148148
            </Address>
149149
        </div>
150150
      </body>
151151
    </html>
152152
    </xsl:template>
153153
</xsl:stylesheet>

ContributeMost Recent Contributions (feed)

Contribute ... Next Page