-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1035.ps1
More file actions
153 lines (135 loc) · 4.89 KB
/
Copy path1035.ps1
File metadata and controls
153 lines (135 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
##################################################
# cmdlets
##################################################
#-------------------------------------------------
# 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 {
# 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 Message Object
$Message = New-Object System.Net.Mail.MailMessage
# Now Populate the Message Object.
$Message.Subject = $Subject
$Message.Body = $Body
$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 $_
}
}
##################################################
# Main
##################################################
Export-ModuleMember Send-HTMLFormattedEmail
### 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>