-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSend-SendGridEmail.ps1
231 lines (176 loc) · 8.21 KB
/
Send-SendGridEmail.ps1
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
#Requires -Version 3.0
Function Send-SendGridEmail {
<#
.SYNOPSIS
This cmdlet is used to send an email using the SendGrid API for use in PowerShell scripting situations
.DESCRIPTION
Send an email using PowerShell with the SendGrid API. If you have more than file to attach you will need to send a second email.
If you have more than one TO email address you will need to send a second email.
.PARAMETER ToAddress
Enter the TO addresses the email will be delivered
.PARAMETER ToName
Enter the TO names where the email will be delivered
.PARAMETER FromAddress
Enter the FROM address the email will be set from
.PARAMETER FromName
Enter the FROM name the email will be sent from
.PARAMETER Subject
Subjet value for the email
.PARAMETER Body
Contents of the email message using Text formatting
.PARAMETER HTMLBody
Contents of the email message written in HTML format
.PARAMETER Attachment
Define a text or html based file(s) to attach to the email
.PARAMETER APIKey
API token used to authenticate to the SendGrid API
.EXAMPLE
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "[email protected]" -FromName "Rob Osborne" -Subject "Test Email" -Body "Hey this is a test email thank you!" -APIKey $APIKey
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name using a Text formatted email body
.EXAMPLE
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "[email protected]" -FromName "Rob Osborne" -Subject "Test Email" -HTMLBody "<br>Hey,<br> <br>This is a test email. <br><br>Thank you!" -Attachment C:\Temp\File.txt -APIKey $APIKey
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name using an HTML formatted email body
.EXAMPLE
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "[email protected]" -FromName "Rob Osborne" -Subject "Test Email" -Body "Hey this is a test email thank you!" -Attachment "C:\Temp\File1.txt","C:\Temp\File2.txt" -APIKey $APIKey
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name using a Text formatted email body and incldues the 2 file attachments
.EXAMPLE
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "[email protected]" -FromName "Rob Osborne" -Subject "Test Email" -HTMLBody "<br>Hey,<br> <br>This is a test email. <br><br>Thank you!" -APIKey $APIKey -Attachment "C:\Temp\File1.txt","C:\Temp\File2.txt"
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name using an HTML formatted email body and incldues the 2 file attachments
.LINK
https://osbornepro.com
https://encrypit.osbornepro.com
https://btpssecpack.osbornepro.com
https://writeups.osbornepro.com
https://github.com/tobor88
https://github.com/OsbornePro
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.hackthebox.eu/profile/52286
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.INPUTS
None
.OUTPUTS
None
#>
param (
[CmdletBinding(DefaultParameterSetName="Text")]
[Parameter(
Position = 0,
Mandatory=$True,
HelpMessage="Enter the TO address the email will be delivered `nEXAMPLE: [email protected] "
)] # End Parameter
[Alias("To")]
[ValidateNotNullOrEmpty()]
[String[]]$ToAddress,
[Parameter(
Position = 1,
Mandatory=$False,
HelpMessage="Enter the TO name where the email will be delivered `nEXAMPLE: John Smith "
)] # End Parameter
[String[]]$ToName,
[Parameter(
Position = 2,
Mandatory=$True,
HelpMessage="REQUIRES A VERIFIED SENDER: `nEnter the FROM address the email will be set from `nEXAMPLE: [email protected] ")] # End Parameter
[Alias("From")]
[ValidateSet("[email protected]","[email protected]","[email protected]","[email protected]","[email protected]")]
[String]$FromAddress,
[Parameter(
Position = 3,
Mandatory=$False,
HelpMessage="Enter the FROM name the email will be sent from `nEXAMPLE: Vinebrook Technology ")] # End Parameter
[String]$FromName,
[Parameter(
Position = 4,
Mandatory=$True,
HelpMessage="Enter a Subject for your email message `nEXAMPLE: Meeting Thursday ")] # End Parameter
[ValidateNotNullOrEmpty()]
[String]$Subject,
[Parameter(
ParameterSetName="Text",
Position = 5,
Mandatory=$True,
HelpMessage="Enter a message to place in the body of your email `nEXAMPLE: Hi John, I look forward to our meeting ")] # End Parameter
[String]$Body,
[Parameter(
ParameterSetName="HTML",
Position = 5,
Mandatory=$True,
HelpMessage="Enter an HTML written message to place in the body of your email `nEXAMPLE: <br>Hi John,<br><br> I look forward to our meeting<br><br> Thanks ")] # End Parameter
[String]$HTMLBody,
[Parameter(
Position = 6,
Mandatory=$False,
HelpMessage="Enter the absolute path to a file you to attach to your email send. `nEXAMPLE: C:\temp\file.txt")] # End Parameter
[ValidateScript({Test-Path -Path $_})]
[String[]]$Attachment,
[Parameter(
Position = 7,
Mandatory=$False,
HelpMessage="Enter your SendGrid API key `nEXAMPLE: SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ")] # End Parameter
[ValidateScript({$_.Length -eq 69 -and $_ -like "SG.*"})]
[String]$APIKey
) # End param
$Count = 0
$AllTo = @()
$MailBodyType = 'text/plain'
$MailBodyValue = $Body
If ($PSBoundParameters.ContainsKey("HTMLBody")) {
$MailbodyType = 'text/HTML'
$MailbodyValue = $HTMLBody
} # End If Else
ForEach ($T in $ToAddress) {
$AllRecipients = @{}
$AllRecipients."email" = $ToAddress[$Count]
$AllRecipients."name" = $ToName[$Count]
$AllTo += $AllRecipients
$Count++
} # End ForEach
If ($PSBoundParameters.ContainsKey("Attachment")) {
$AllAttachments = @()
ForEach ($A in $Attachment) {
$NewA = @{}
$FileName = $A.Split("\")[-1]
$Base64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes($A))
$NewA."content"=$Base64
$NewA."filename"=$FileName
$NewA."type"="text/html"
$NewA."disposition"="attachment"
$AllAttachments += $NewA
} # End ForEach
} # End If
$SendGridBody = @{
"personalizations" = @(
@{
"to" = @(
$AllTo
)
"subject" = $Subject
}
) # End personalizations
"content" = @(
@{
"type" = $MailBodyType
"value" = $MailBodyValue
}
) # End content
"from" = @{
"email" = $FromAddress
"name" = $FromName
} # End from
} # End $SendGridBody
If ($AllAttachments) {
$SendGridBody.attachments = @($AllAttachments)
} # End If
$BodyJson = $SendGridBody | ConvertTo-Json -Depth 10
$Header = @{
"Authorization" = "Bearer $APIKey"
} # End $Header
Write-Verbose -Message "[v] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Sending email through SendGrid SMTP server"
Invoke-RestMethod -Method POST -Uri "https://api.sendgrid.com/v3/mail/send" -Headers $Header -Body $BodyJson -ContentType "application/json"
} # End Send-SendGridEmail