-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendgridwrapper.psm1
359 lines (317 loc) · 15.2 KB
/
Sendgridwrapper.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<#PSSCRIPTInfo
.SYNOPSIS
Handles data formatting to send an email through SendGrid. Supports adding a CSV attachment and basic 3-part TITLE/MAINTEXT/ENDTEXT HTML template or plain text one liner, for instance "this is an automated email message".
Used for automated email messages associated with scripts. Requires an API_KEY which should be an Azure Runbook encrypted variable for Azure Automation, an Azure Keyvault Secret for Azure Functions, or a SecureString.
.DESCRIPTION
Send an email through sendgrid's v3 SendMail API.
Parameter Splatting is highly recommanded to increase readability of the script (About_Splatting)
.INPUTS
System.Object[]
.OUTPUTS
[System.Collections.Hashtable]
.NOTES
Version 1.14
Author Marcellin Penicaud (mpe@openhost.io)
Creation Date 19/11/2019
This is an old script I did, which could use many many improvements. I learned a lot writing it and it saved me a great amount of time.
.PARAMETER APIKEY
If the script runs from an Azure Runbook, the API key should be stored as an encrypted variable and called with the "Get-AutomationVariable -Name 'APIKEY-Sendgrid'" command.
If the script runs from an Azure Function, the API Key should be stored in an Az Keyvault and called with the "(Get-AzKeyVaultSecret -SecretName APIKEY -VaultName <VaultName>).SecretValue" command.
The System Managed Identity must have access to the keyvault and the read secret authorization.
.PARAMETER TEXT
-TEXT is only mandatory if not using HTML (-UseHTML $true).
.SYNTAX
Send-GridMailMessage -APIKEY <secure.string> -From <string> -To <array> -Subject <string> -Text <string> -ReplyToDisplayName <string> -ReplayToMailAddress <string> [-Bcc <array>] [<CommonParameters>]
Send-GridMailMessage -APIKEY <secure.string> -From <string> -To <array> -Subject <string> -AttachmentFileName <string> -Text <string> -ReplyToDisplayName <string> -ReplayToMailAddress <string> [-Bcc <array>] [-AttachmentPath <Object>] [-UseHTML <bool>] [-HTMLTitle <string>] [-HTMLMainText <string>] [-HTMLEndText <string>] [<CommonParameters>]
Send-GridMailMessage -APIKEY <secure.string> -From <string> -To <array> -Subject <string> -AttachmentFileName <string> -Text <string> -ReplyToDisplayName <string> -ReplayToMailAddress <string> [-Bcc <array>] [-AttachmentContent <Object>] [-UseHTML <bool>] [-HTMLTitle <string>] [-HTMLMainText <string>] [-HTMLEndText <string>] [<CommonParameters>]
Send-GridMailMessage -APIKEY <secure.string> -From <string> -To <array> -Subject <string> -UseHTML <bool> -HTMLTitle <string> -HTMLMainText <string> -ReplyToDisplayName <string> -ReplayToMailAddress <string> [-Bcc <array>] [-HTMLEndText <string>] [<CommonParameters>]
.EXAMPLE
$SimpleParams = @{
APIKEY = (Get-AutomationVariable -Name 'APIKEY')
Subject = $Subject
To = $To
Bcc = $Bcc
Text = $TextString
From = $From
}
Send-GridMailMessage @SimpleParams
.EXAMPLE
$Arrayparams = @{
APIKEY = (Get-AutomationVariable -Name 'APIKEY')
Subject = $Subject
To = [array]$To
From = $From
Bcc = [array]$Bcc
Text = $Text
}
Send-GridMailMessage @Arrayparams
.EXAMPLE
$HTMLParams = @{
APIKEY = (Get-AutomationVariable -Name 'APIKEY')
Subject = $Subject
To = $ToString
Bcc = $BccString
From = $From
UseHTML = $true
HTMLTitle = $HTMLTitle
HTMLMaintext = $HTMLMaintext
HTMLEndText = $HTMLEndText
}
Send-GridMailMessage @HTMLParams
.EXAMPLE
$AttachmentFileParams = @{
APIKEY = $APIKEY
To = $ToString
Subject = $Subject
Bcc = $BccString
Text = $TextString
From = $From
AttachmentFile = $AttachmentFile
AttachmentFileName = "Attachment-File-name"
}
Send-GridMailMessage @AttachmentFileParams
.EXAMPLE
$AttachmentContentParams = @{
APIKEY = $APIKEY
To = $ToString
Subject = $Subject
Bcc = $BccString
Text = $TextString
From = $From
AttachmentContent = $AttachmentContent
AttachmentFileName = "Attachment-File-name"
}
Send-GridMailMessage @AttachmentContentParams
#>
Function Send-GridMailMessage {
[cmdletbinding(DefaultParameterSetName = 'none')]
Param(
[Parameter (Mandatory)]
[System.Security.SecureString] $APIKEY,
[Parameter (Mandatory)]
[ValidateNotNullorEmpty()]
[Validatescript( {
if ($_ -like "*@*.*") {
$true
}
else {
throw "$_ is not a valid sender email address. -From Must match *@*.* pattern."
}
}
)]
[String] $From,
[Parameter (Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateLength(1, 320)]
[array] $To,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[array] $Bcc,
[Parameter (Mandatory)]
[ValidateNotNullOrEmpty()]
[String] $Subject,
[Parameter(Mandatory = $false,
ParameterSetName = 'Attachment')]
[ValidateNotNullOrEmpty()]
$AttachmentPath,
[parameter(ParameterSetName = "Attachment_Content")]
[ValidateNotNullOrEmpty()]
$AttachmentContent,
[Parameter(ParameterSetName = 'Attachment_Content',
Mandatory)]
[Parameter(ParameterSetName = 'Attachment',
Mandatory)]
[ValidateNotNullorEmpty()]
[String] $AttachmentFileName,
[Parameter(ParameterSetName = 'html',
Mandatory)]
[Parameter(ParameterSetName = 'Attachment_Content')]
[Parameter(ParameterSetName = 'Attachment')]
[ValidateNotNullOrEmpty()]
[bool] $UseHTML = $false,
# Text is mandatory unless HTML is used, in which case it is forbidden.
[Parameter(ParameterSetname = 'none',
Mandatory)]
[Parameter(ParameterSetName = 'Attachment_Content',
Mandatory = $false)]
[Parameter(ParameterSetName = 'Attachment',
Mandatory = $false)]
[ValidateNotNullorEmpty()]
[String] $Text,
[Parameter(ParameterSetName = 'html',
Mandatory)]
[Parameter(ParameterSetName = 'Attachment_Content')]
[Parameter(ParameterSetName = 'Attachment')]
[ValidateNotNullOrEmpty()]
[String] $HTMLTitle,
[Parameter(ParameterSetName = 'html',
Mandatory)]
[Parameter(ParameterSetName = 'Attachment_Content')]
[Parameter(ParameterSetName = 'Attachment')]
[ValidateNotNullOrEmpty()]
[String] $HTMLMainText,
[Parameter(ParameterSetName = 'html',
Mandatory = $false)]
[Parameter(ParameterSetName = 'Attachment_Content')]
[Parameter(ParameterSetName = 'Attachment')]
[ValidateNotNullOrEmpty()]
[String] $HTMLEndText = "This is an automated email message, please reply to ",
[Parameter(ParameterSetName = 'html', Mandatory)]
[Parameter(ParameterSetName = 'Attachment_Content', Mandatory)]
[Parameter(ParameterSetName = 'Attachment', Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $ReplyToMailAddress,
[parameter(ParameterSetName = 'none', Mandatory)]
[Parameter(ParameterSetName = 'html', Mandatory)]
[Parameter(ParameterSetName = 'Attachment_Content', Mandatory)]
[Parameter(ParameterSetName = 'Attachment', Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $ReplyToDisplayName
)
### CRAFT HEADERS ###
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer " + $APIKEY)
$headers.Add("Content-Type", "application/json")
### Encode Attachment to Base64 ###
# SendGrid API expects content as base64 encoded https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
# This command encodes the FILE to a single base64 string
# If attachment is a file, encode it to Base64. Else, convert the data to CSV in temp folder, then convert the content to base64.
If ($AttachmentPath) {
$base64String = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($AttachmentPath))
}
Else {
if ($AttachmentFileName -notlike "*.csv") {
$TempFilePath = (New-Item -type File -Path "$AttachmentFileName.csv").FullName
$AttachmentContent | Export-CSV -Path $TempFilePath -Force -Delimiter ";" -NoTypeInformation -Encoding utf8
$base64String = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($TempFilePath))
}
Else {
$TempFilePath = (New-Item -type File -path "$AttachmentFileName").Fullname
$AttachmentContent | Export-CSV -Path $TempFilePath -Force -Delimiter ";" -NoTypeInformation -Encoding utf8
$base64String = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($TempFilePath))
}
}
### Transform parameters to array objects that will be used in the body ###
# Craft the recipients
$JSON_To = [array]($To | ForEach-Object {
@{ email = $_ }
})
# Craft the BCC
$JSON_Bcc = [array]($Bcc | ForEach-Object {
@{ email = $_ }
})
If ($UseHTML -eq $false ) {
[String]$EmailContentType = "text/plain"
}
if ($UseHTML -eq $true) {
[String]$EmailContentType = "text/html"
}
### HTML ###
# Create a here-string containing a Standard, Simple looking HTML 3 lines text. Default Content should be TITLE + CONTENT + Default noreply message, but that's not mandatory.
# Line breaks are supported in the form <br> in the $text parameter.
If ($UseHTML -eq $true) {
$HTMLContent = @"
<html>
<head></head>
<body class="em_body" style="margin:0px; padding:0px;" bgcolor="#efefef">
<table class="em_full_wrap" valign="top" width="100%" cellspacing="0" cellpadding="0" border="0" bgcolor="#efefef"
align="center">
<tbody>
<tr>
<td valign="top" align="center">
<table class="em_main_table" style="width:700px;" width="700" cellspacing="0" cellpadding="0"
border="0" align="center">
<tbody>
<tr>
<td style="padding:35px 70px 30px;" class="em_padd" valign="top" bgcolor="#0d1121"
align="center">
<table width="100%" cellspacing="0" cellpadding="0" border="0" align="center">
<tbody>
<tr>
<td style="font-family:'Open Sans', Arial, sans-serif; font-size:18px; line-height:22px; color:#fbeb59; text-transform:uppercase; letter-spacing:2px; padding-bottom:12px;"
valign="top" align="center">$HTMLTitle</td>
</tr>
<tr>
<td style="font-size:0px; line-height:0px; height:15px;" height="15">
</td>
</tr>
<tr>
<td style="font-family:'Open Sans', Arial, sans-serif; font-size:18px; line-height:22px; color:#fbeb59; letter-spacing:2px; padding-bottom:12px;"
valign="top" align="center">$HTMLMainText
</td>
</tr>
<tr>
<td class="em_h20" style="font-size:0px; line-height:0px; height:25px;"
height="25"> </td>
</tr>
<tr>
<td style="font-family:'Open Sans', Arial, sans-serif; font-size:18px; line-height:22px; color:#fbeb59; text-transform:uppercase; letter-spacing:2px; padding-bottom:12px;"
valign="top" align="center">$HTMLEndText
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<div class="em_hide" style="white-space: nowrap; display: none; font-size:0px; line-height:0px;">
</div>
</body>
</html>
"@
#Remove special characters
$HTMLContent = [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($HTMLContent))
#Encode $HTMLContent to UTF-8
$HTMLContent = [Text.Encoding]::UTF8.GetString([text.encoding]::ASCII.GetBytes($HTMLContent))
$text = $HTMLContent
}
# Craft the body.
# Reply_to has been added in case someone tries to answer the email as end-users will receive them. Domain should match in $from and $reply_to to domains.
$body = @{
personalizations = @(
@{
to = $JSON_To
}
)
from = @{
email = $from
}
reply_to = @{
email = $ReplyToMailAddress
name = $ReplyToDisplayName
}
subject = $subject
content = @(
@{
type = $EmailContentType
value = $Text
}
)
}
# Add the BCC if it exists
If ($null -ne $BCC) {
$Body.personalizations[0].Add("Bcc", $JSON_Bcc)
}
# Add the attachment to the $Body if it exists
if ($null -ne $AttachmentPath -or $null -ne $AttachmentContent) {
$body["attachments"] = @(
@{
filename = $AttachmentFileName
content = $base64String
disposition = "attachment"
type = "text/csv"
}
)
}
# Converts the body to JSON
$BodyJson = $body | ConvertTo-Json -Depth 4
Write-Verbose $BodyJson
# Sends the email through SendGrid API
$Output = Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $headers -Body $bodyJson
Write-Output $Output
}