Emailimo.psm1

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
function Email {
    [CmdLetBinding(SupportsShouldProcess = $True)]
    param([Parameter(Mandatory = $false, Position = 0)][ScriptBlock] $Email,
        [string[]] $To,
        [string[]] $CC,
        [string[]] $BCC,
        [string] $ReplyTo,
        [string] $From,
        [string] $Subject,
        [alias('SelfAttach')][switch] $AttachSelf,
        [string] $AttachSelfName,
        [string] $Server,
        [string] $Username,
        [int] $Port = 587,
        [string] $Password,
        [switch] $PasswordFromFile,
        [switch] $PasswordAsSecure,
        [switch] $SSL,
        [ValidateSet('Low', 'Normal', 'High')] [string] $Priority = 'Normal',
        [ValidateSet('None', 'OnSuccess', 'OnFailure', 'Delay', 'Never')] $DeliveryNotifications = 'None',
        [string] $Encoding = 'Unicode',
        [string] $FilePath,
        [bool] $Supress = $true)
    $StartTime = Start-TimeLog
    $ServerParameters = [ordered] @{From = $From
        To = $To
        CC = $CC
        BCC = $BCC
        ReplyTo = $ReplyTo
        Server = $Server
        Login = $Username
        Password = $Password
        PasswordAsSecure = $PasswordAsSecure
        PasswordFromFile = $PasswordFromFile
        Port = $Port
        EnableSSL = $SSL
        Encoding = $Encoding
        Subject = $Subject
        Priority = $Priority
        DeliveryNotifications = $DeliveryNotifications
    }
    $Attachments = [System.Collections.Generic.List[string]]::new()
    $Body = New-HTML -UseCssLinks -UseJavaScriptLinks { [Array] $EmailParameters = Invoke-Command -ScriptBlock $Email
        foreach ($Parameter in $EmailParameters) {
            switch ($Parameter.Type) {
                HeaderTo { $ServerParameters.To = $Parameter.Addresses }
                HeaderCC { $ServerParameters.CC = $Parameter.Addresses }
                HeaderBCC { $ServerParameters.BCC = $Parameter.Addresses }
                HeaderFrom { $ServerParameters.From = $Parameter.Address }
                HeaderReplyTo { $ServerParameters.ReplyTo = $Parameter.Address }
                HeaderSubject { $ServerParameters.Subject = $Parameter.Subject }
                HeaderServer {
                    $ServerParameters.Server = $Parameter.Server
                    $ServerParameters.Port = $Parameter.Port
                    $ServerParameters.Login = $Parameter.UserName
                    $ServerParameters.Password = $Parameter.Password
                    $ServerParameters.PasswordFromFile = $Parameter.PasswordFromFile
                    $ServerParameters.PasswordAsSecure = $Parameter.PasswordAsSecure
                    $ServerParameters.EnableSSL = $Parameter.SSL
                }
                HeaderAttachment { foreach ($Attachment in $Parameter.FilePath) { $Attachments.Add($Attachment) } }
                HeaderOptions {
                    $ServerParameters.DeliveryNotifications = $Parameter.DeliveryNotifications
                    $ServerParameters.Encoding = $Parameter.Encoding
                    $ServerParameters.Priority = $Parameter.Priority
                }
                Default { $Parameter }
            }
        } }
    if ($FilePath) { Save-HTML -FilePath $FilePath -HTML $Body }
    if ($AttachSelf) {
        if ($AttachSelfName) { $TempFilePath = "$(Get-TemporaryDirectory)\$($AttachSelfName).html" } else { $TempFilePath = '' }
        $Saved = Save-HTML -FilePath $TempFilePath -HTML $Body -Supress $false
        if ($Saved) { $Attachments.Add($Saved) }
    }
    $MailSentTo = "To: $($ServerParameters.To -join ', '); CC: $($ServerParameters.CC -join ', '); BCC: $($ServerParameters.BCC -join ', ')".Trim()
    if ($pscmdlet.ShouldProcess("$MailSentTo", "Email")) {
        $EmailOutput = Send-Email -EmailParameters $ServerParameters -Body ($Body -join '') -Attachment $Attachments
        if (-not $Supress) { $EmailOutput }
    }
    $EndTime = Stop-TimeLog -Time $StartTime -Option OneLiner
    Write-Verbose "Email - Time to send: $EndTime"
}
function EmailAttachment {
    [CmdletBinding()]
    param([string[]] $FilePath)
    [PSCustomObject] @{Type = 'HeaderAttachment'
        FilePath = $FilePath
    }
}
function EmailBCC {
    [CmdletBinding()]
    param([string[]] $Addresses)
    [PsCustomObject] @{Type = 'HeaderBCC'
        Addresses = $Addresses
    }
}
function EmailBody {
    [CmdletBinding()]
    param([Parameter(Mandatory = $false, Position = 0)][ScriptBlock] $EmailBody,
        [RGBColors] $Color,
        [RGBColors] $BackGroundColor,
        [alias('Size')][int] $FontSize,
        [ValidateSet('normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900')][string] $FontWeight,
        [ValidateSet('normal', 'italic', 'oblique')][string] $FontStyle,
        [ValidateSet('normal', 'small-caps')][string] $FontVariant,
        [string] $FontFamily ,
        [ValidateSet('left', 'center', 'right', 'justify')][string] $Alignment,
        [ValidateSet('none', 'line-through', 'overline', 'underline')][string] $TextDecoration,
        [ValidateSet('uppercase', 'lowercase', 'capitalize')][string] $TextTransform,
        [ValidateSet('rtl')][string] $Direction)
    $newHTMLSplat = @{ }
    if ($Alignment) { $newHTMLSplat.Alignment = $Alignment }
    if ($FontSize) { $newHTMLSplat.FontSize = $FontSize }
    if ($TextTransform) { $newHTMLSplat.TextTransform = $TextTransform }
    if ($Color) { $newHTMLSplat.Color = $Color }
    if ($FontFamily) { $newHTMLSplat.FontFamily = $FontFamily }
    if ($Direction) { $newHTMLSplat.Direction = $Direction }
    if ($FontStyle) { $newHTMLSplat.FontStyle = $FontStyle }
    if ($TextDecoration) { $newHTMLSplat.TextDecoration = $TextDecoration }
    if ($BackGroundColor) { $newHTMLSplat.BackGroundColor = $BackGroundColor }
    if ($FontVariant) { $newHTMLSplat.FontVariant = $FontVariant }
    if ($FontWeight) { $newHTMLSplat.FontWeight = $FontWeight }
    [bool] $SpanRequired = $false
    foreach ($Entry in $newHTMLSplat.GetEnumerator()) {
        if ((Get-ObjectCount -Object $Entry.Value) -gt 0) {
            $SpanRequired = $true
            break
        }
    }
    if ($SpanRequired) { New-HTMLSpanStyle @newHTMLSplat { Invoke-Command -ScriptBlock $EmailBody } } else { Invoke-Command -ScriptBlock $EmailBody }
}
function EmailCC {
    [CmdletBinding()]
    param([string[]] $Addresses)
    [PsCustomObject] @{Type = 'HeaderCC'
        Addresses = $Addresses
    }
}
function EmailFrom {
    [CmdletBinding()]
    param([string] $Address)
    [PsCustomObject] @{Type = 'HeaderFrom'
        Address = $Address
    }
}
function EmailHeader {
    [CmdletBinding()]
    param([Parameter(Mandatory = $false, Position = 0)][ScriptBlock] $EmailHeader)
    $EmailHeaders = Invoke-Command -ScriptBlock $EmailHeader
    $EmailHeaders
}
function EmailHTML {
    [CmdletBinding()]
    param([ScriptBlock] $HTML)
    Invoke-Command -ScriptBlock $HTML
}
function EmailList {
    [CmdletBinding()]
    param([ScriptBlock]$ListItems,
        [ValidateSet('Unordered', 'Ordered')] [string] $Type = 'Unordered',
        [RGBColors] $Color,
        [RGBColors] $BackGroundColor,
        [int] $FontSize,
        [ValidateSet('normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900')][string] $FontWeight,
        [ValidateSet('normal', 'italic', 'oblique')][string] $FontStyle,
        [ValidateSet('normal', 'small-caps')][string] $FontVariant,
        [string] $FontFamily ,
        [ValidateSet('left', 'center', 'right', 'justify')][string] $Alignment,
        [ValidateSet('none', 'line-through', 'overline', 'underline')][string] $TextDecoration,
        [ValidateSet('uppercase', 'lowercase', 'capitalize')][string] $TextTransform,
        [ValidateSet('rtl')][string] $Direction,
        [switch] $LineBreak)
    $newHTMLSplat = @{ }
    if ($Alignment) { $newHTMLSplat.Alignment = $Alignment }
    if ($FontSize) { $newHTMLSplat.FontSize = $FontSize }
    if ($TextTransform) { $newHTMLSplat.TextTransform = $TextTransform }
    if ($Color) { $newHTMLSplat.Color = $Color }
    if ($FontFamily) { $newHTMLSplat.FontFamily = $FontFamily }
    if ($Direction) { $newHTMLSplat.Direction = $Direction }
    if ($FontStyle) { $newHTMLSplat.FontStyle = $FontStyle }
    if ($TextDecoration) { $newHTMLSplat.TextDecoration = $TextDecoration }
    if ($BackGroundColor) { $newHTMLSplat.BackGroundColor = $BackGroundColor }
    if ($FontVariant) { $newHTMLSplat.FontVariant = $FontVariant }
    if ($FontWeight) { $newHTMLSplat.FontWeight = $FontWeight }
    if ($LineBreak) { $newHTMLSplat.LineBreak = $LineBreak }
    New-HTMLList @newHTMLSplat -Type $Type { Invoke-Command -ScriptBlock $ListItems }
}
function EmailListItem {
    [CmdletBinding()]
    param([string[]] $Text,
        [RGBColors[]] $Color = @(),
        [RGBColors[]] $BackGroundColor = @(),
        [int[]] $FontSize = @(),
        [ValidateSet('normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900')][string[]] $FontWeight = @(),
        [ValidateSet('normal', 'italic', 'oblique')][string[]] $FontStyle = @(),
        [ValidateSet('normal', 'small-caps')][string[]] $FontVariant = @(),
        [string[]] $FontFamily = @(),
        [ValidateSet('left', 'center', 'right', 'justify')][string[]] $Alignment = @(),
        [ValidateSet('none', 'line-through', 'overline', 'underline')][string[]] $TextDecoration = @(),
        [ValidateSet('uppercase', 'lowercase', 'capitalize')][string[]] $TextTransform = @(),
        [ValidateSet('rtl')][string[]] $Direction = @(),
        [switch] $LineBreak)
    $newHTMLTextSplat = @{Alignment = $Alignment
        FontSize = $FontSize
        TextTransform = $TextTransform
        Text = $Text
        Color = $Color
        FontFamily = $FontFamily
        Direction = $Direction
        FontStyle = $FontStyle
        TextDecoration = $TextDecoration
        BackGroundColor = $BackGroundColor
        FontVariant = $FontVariant
        FontWeight = $FontWeight
        LineBreak = $LineBreak
    }
    New-HTMLListItem @newHTMLTextSplat
}
function EmailOptions {
    [CmdletBinding()]
    param([ValidateSet('Low', 'Normal', 'High')] [string] $Priority = 'Normal',
        [ValidateSet('None', 'OnSuccess', 'OnFailure', 'Delay', 'Never')] $DeliveryNotifications = 'None',
        [string] $Encoding = 'Unicode')
    [PsCustomObject] @{Type = 'HeaderOptions'
        Encoding = $Encoding
        DeliveryNotifications = $DeliveryNotifications
        Priority = $Priority
    }
}
function EmailReplyTo {
    [CmdletBinding()]
    param([string] $Address)
    [PsCustomObject] @{Type = 'HeaderReplyTo'
        Address = $Address
    }
}
function EmailServer {
    [CmdletBinding()]
    param([string] $Server,
        [int] $Port = 587,
        [string] $UserName,
        [string] $Password,
        [switch] $PasswordAsSecure,
        [switch] $PasswordFromFile,
        [switch] $SSL)
    [PsCustomObject] @{Type = 'HeaderServer'
        Server = $Server
        Port = $Port
        UserName = $UserName
        Password = $Password
        PasswordAsSecure = $PasswordAsSecure
        PasswordFromFile = $PasswordFromFile
        SSL = $SSL
    }
}
function EmailSubject {
    [CmdletBinding()]
    param([string] $Subject)
    [PsCustomObject] @{Type = 'HeaderSubject'
        Subject = $Subject
    }
}
function EmailText {
    [CmdletBinding()]
    param([Parameter(Mandatory = $false, Position = 0)][ScriptBlock] $TextBlock,
        [string[]] $Text,
        [RGBColors[]] $Color = @(),
        [RGBColors[]] $BackGroundColor = @(),
        [alias('Size')][int[]] $FontSize = @(),
        [ValidateSet('normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900')][string[]] $FontWeight = @(),
        [ValidateSet('normal', 'italic', 'oblique')][string[]] $FontStyle = @(),
        [ValidateSet('none', 'line-through', 'overline', 'underline')][string[]] $TextDecoration = @(),
        [ValidateSet('normal', 'small-caps')][string[]] $FontVariant = @(),
        [string[]] $FontFamily = @(),
        [ValidateSet('left', 'center', 'right', 'justify')][string[]] $Alignment = @(),
        [ValidateSet('uppercase', 'lowercase', 'capitalize')][string[]] $TextTransform = @(),
        [ValidateSet('rtl')][string[]] $Direction = @(),
        [switch] $LineBreak,
        [switch] $SkipParagraph)
    if ($TextBlock) { $Text = (Invoke-Command -ScriptBlock $TextBlock) }
    $newHTMLTextSplat = @{Alignment = $Alignment
        FontSize = $FontSize
        TextTransform = $TextTransform
        Text = $Text
        Color = $Color
        FontFamily = $FontFamily
        Direction = $Direction
        FontStyle = $FontStyle
        TextDecoration = $TextDecoration
        BackGroundColor = $BackGroundColor
        FontVariant = $FontVariant
        FontWeight = $FontWeight
        LineBreak = $LineBreak
        SkipParagraph = $SkipParagraph
    }
    New-HTMLText @newHTMLTextSplat
}
function EmailTextBox {
    [CmdletBinding()]
    param([Parameter(Mandatory = $false, Position = 0)][ScriptBlock] $TextBlock,
        [RGBColors[]] $Color = @(),
        [RGBColors[]] $BackGroundColor = @(),
        [alias('Size')][int[]] $FontSize = @(),
        [ValidateSet('normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900')][string[]] $FontWeight = @(),
        [ValidateSet('normal', 'italic', 'oblique')][string[]] $FontStyle = @(),
        [ValidateSet('none', 'line-through', 'overline', 'underline')][string[]] $TextDecoration = @(),
        [ValidateSet('normal', 'small-caps')][string[]] $FontVariant = @(),
        [string[]] $FontFamily = @(),
        [ValidateSet('left', 'center', 'right', 'justify')][string[]] $Alignment = @(),
        [ValidateSet('uppercase', 'lowercase', 'capitalize')][string[]] $TextTransform = @(),
        [ValidateSet('rtl')][string[]] $Direction = @(),
        [switch] $LineBreak)
    if ($TextBlock) {
        $Text = (Invoke-Command -ScriptBlock $TextBlock)
        if ($Text.Count) { $LineBreak = $true }
    }
    foreach ($T in $Text) {
        $newHTMLTextSplat = @{Alignment = $Alignment
            FontSize = $FontSize
            TextTransform = $TextTransform
            Text = $T
            Color = $Color
            FontFamily = $FontFamily
            Direction = $Direction
            FontStyle = $FontStyle
            TextDecoration = $TextDecoration
            BackGroundColor = $BackGroundColor
            FontVariant = $FontVariant
            FontWeight = $FontWeight
            LineBreak = $LineBreak
        }
        New-HTMLText @newHTMLTextSplat -SkipParagraph
    }
}
function EmailTo {
    [CmdletBinding()]
    param([string[]] $Addresses)
    [PsCustomObject] @{Type = 'HeaderTo'
        Addresses = $Addresses
    }
}
Export-ModuleMember -Function @('Email', 'EmailAttachment', 'EmailBCC', 'EmailBody', 'EmailCC', 'EmailFrom', 'EmailHeader', 'EmailHTML', 'EmailList', 'EmailListItem', 'EmailOptions', 'EmailReplyTo', 'EmailServer', 'EmailSubject', 'EmailText', 'EmailTextBox', 'EmailTo') -Alias @()