-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSendGridFunction.ps1
134 lines (114 loc) · 3.76 KB
/
SendGridFunction.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
function Send-SendGridEmail {
<#
.SYNOPSIS
Function to send email with Twilio SendGrid
.DESCRIPTION
A function to send a text or HTML based email with PowerShell and SendGrid.
See https://docs.sendgrid.com/api-reference/mail-send/mail-send for API details.
Update the API key and from address in the function before using.
This script provided as-is with no warranty. Test it before you trust it.
www.ciraltos.com
.PARAMETER destEmailAddress
The destination email address.
.PARAMETER subject
The subject of the email.
.PARAMETER contentType
The content type, values are “text/plain” or “text/html”. "text/plain" set by default.
.PARAMETER contentBody
The HTML or plain text content that of the email.
.NOTES
Version: 2.0
Author: Travis Roberts
Creation Date: 12/1/2022
Purpose/Change: Update for latest release of Twilio SendGrid
**** Update the API key and from email address with matching settings from SendGrid ****
**** From address must be validated to before email delivery ****
**** This script provided as-is with no warranty. Test it before you trust it. ****
.Example
See my YouTube channel at http://www.youtube.com/c/TravisRoberts or https://www.ciraltos.com for details.
#>
param(
[Parameter(Mandatory = $true)]
[String] $destEmailAddress,
[Parameter(Mandatory = $true)]
[String] $subject,
[Parameter(Mandatory = $false)]
[string]$contentType = 'text/plain',
[Parameter(Mandatory = $true)]
[String] $contentBody
)
############ Update with your SendGrid API Key and Verified Email Address ####################
$apiKey = "<SENDGRID_API_KEY>"
$fromEmailAddress = "<VERIFIED_EMAIL_ADDRESS>"
$headers = @{
'Authorization' = 'Bearer ' + $apiKey
'Content-Type' = 'application/json'
}
$body = @{
personalizations = @(
@{
to = @(
@{
email = $destEmailAddress
}
)
}
)
from = @{
email = $fromEmailAddress
}
subject = $subject
content = @(
@{
type = $contentType
value = $contentBody
}
)
}
try {
$bodyJson = $body | ConvertTo-Json -Depth 4
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error converting body to json ' + $ErrorMessage)
Break
}
try {
Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $headers -Body $bodyJson
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error with Invoke-RestMethod ' + $ErrorMessage)
Break
}
}
# Examples, call the function using splat to pass in parameters:
# Sample code with plain text
$splat = @{
destEmailAddress = '<To_Email_Address>'
subject = 'Test Email'
contentBody = 'This is a test message sent from SendGrid'
}
Send-SendGridEmail @splat
# Sample code with HTML body
$htmlBody = @"
<table>
<tr>
<header>
<h1 align="center">This is the Header</h1>
</header>
<tr>
<td align="center">Here is the content of my message</td>
</tr>
<tr>
<td align="center">This is the footer</td>
</tr>
</table>
"@
$splat = @{
destEmailAddress = '<To_Email_Address>'
subject = 'Test Email'
contentType = 'text/html'
contentBody = $htmlBody
}
Send-SendGridEmail @splat