Skip to content

Commit 87a49bb

Browse files
authored
Update Send-SendGridEmail.ps1
1 parent 329a237 commit 87a49bb

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

Send-SendGridEmail.ps1

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#Requires -Version 3.0
12
Function Send-SendGridEmail {
23
<#
34
.SYNOPSIS
@@ -10,10 +11,10 @@ If you have more than one TO email address you will need to send a second email.
1011
1112
1213
.PARAMETER ToAddress
13-
Enter the TO address the email will be delivered
14+
Enter the TO addresses the email will be delivered
1415
1516
.PARAMETER ToName
16-
Enter the TO name where the email will be delivered
17+
Enter the TO names where the email will be delivered
1718
1819
.PARAMETER FromAddress
1920
Enter the FROM address the email will be set from
@@ -31,42 +32,35 @@ Contents of the email message using Text formatting
3132
Contents of the email message written in HTML format
3233
3334
.PARAMETER Attachment
34-
Define a text or html based file to attach to the email
35+
Define a text or html based file(s) to attach to the email
3536
3637
.PARAMETER APIKey
3738
API token used to authenticate to the SendGrid API
3839
3940
4041
.EXAMPLE
41-
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "advisor360@vinebrookmsp.com" -FromName "Test Email from Advisor360" -Subject "Test Email" -Body "Hey this is a test email thank you!" -APIKey $APIKey
42-
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name from [email protected]|Test Email from Advisor360 using a Text formatted email body
42+
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "sender@osbornepro.com" -FromName "Rob Osborne" -Subject "Test Email" -Body "Hey this is a test email thank you!" -APIKey $APIKey
43+
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name using a Text formatted email body
4344
4445
.EXAMPLE
45-
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "advisor360@vinebrookmsp.com" -FromName "Test Email from Advisor360" -Subject "Test Email" -HTMLBody "<br>Hey,<br> <br>This is a test email. <br><br>Thank you!" -Attachment C:\Temp\File.txt -APIKey $APIKey
46-
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name from [email protected]|Test Email from Advisor360 using an HTML formatted email body
46+
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "sender@osbornepro.com" -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
47+
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name using an HTML formatted email body
4748
4849
.EXAMPLE
49-
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "advisor360@vinebrookmsp.com" -FromName "Test Email from Advisor360" -Subject "Test Email" -Body "Hey this is a test email thank you!" -Attachment "C:\Temp\File1.txt","C:\Temp\File2.txt" -APIKey $APIKey
50-
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name from [email protected]|Test Email from Advisor360 using a Text formatted email body and incldues the 2 file attachments
50+
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "sender@osbornepro.com" -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
51+
# 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
5152
5253
.EXAMPLE
53-
Send-SendGridEmail -To "[email protected]","[email protected]" -ToName "Recipient1 Name","Recipient 2 Name" -FromAddress "[email protected]" -FromName "Test Email from Advisor360" -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"
54-
# This example sends an email to [email protected]|Recipient 1 Name and [email protected]|Recipient 2 Name from [email protected]|Test Email from Advisor360 using an HTML formatted email body and incldues the 2 file attachments
55-
56-
57-
.INPUTS
58-
None
59-
60-
61-
.OUTPUTS
62-
None
54+
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"
55+
# 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
6356
6457
6558
.LINK
6659
https://osbornepro.com
6760
https://encrypit.osbornepro.com
6861
https://btpssecpack.osbornepro.com
6962
https://writeups.osbornepro.com
63+
https://github.com/tobor88
7064
https://github.com/OsbornePro
7165
https://gitlab.com/tobor88
7266
https://www.powershellgallery.com/profiles/tobor
@@ -79,45 +73,50 @@ https://www.credly.com/users/roberthosborne/badges
7973
Author: Robert H. Osborne
8074
Alias: tobor
8175
76+
77+
78+
.INPUTS
79+
None
80+
81+
82+
.OUTPUTS
83+
None
8284
#>
8385
param (
8486
[CmdletBinding(DefaultParameterSetName="Text")]
8587
[Parameter(
8688
Position = 0,
8789
Mandatory=$True,
88-
ValueFromPipeline=$False,
89-
HelpMessage="Enter the TO address the email will be delivered `nEXAMPLE: [email protected] ")] # End Parameter
90+
HelpMessage="Enter the TO address the email will be delivered `nEXAMPLE: [email protected] "
91+
)] # End Parameter
9092
[Alias("To")]
9193
[ValidateNotNullOrEmpty()]
9294
[String[]]$ToAddress,
9395

9496
[Parameter(
9597
Position = 1,
9698
Mandatory=$False,
97-
ValueFromPipeline=$False,
98-
HelpMessage="Enter the TO name where the email will be delivered `nEXAMPLE: John Smith ")] # End Parameter
99+
HelpMessage="Enter the TO name where the email will be delivered `nEXAMPLE: John Smith "
100+
)] # End Parameter
99101
[String[]]$ToName,
100102

101103
[Parameter(
102104
Position = 2,
103105
Mandatory=$True,
104-
ValueFromPipeline=$False,
105106
HelpMessage="REQUIRES A VERIFIED SENDER: `nEnter the FROM address the email will be set from `nEXAMPLE: [email protected] ")] # End Parameter
106107
[Alias("From")]
107-
[ValidateScript($_ -like "*@*")]
108+
108109
[String]$FromAddress,
109110

110111
[Parameter(
111112
Position = 3,
112113
Mandatory=$False,
113-
ValueFromPipeline=$False,
114114
HelpMessage="Enter the FROM name the email will be sent from `nEXAMPLE: Vinebrook Technology ")] # End Parameter
115115
[String]$FromName,
116116

117117
[Parameter(
118118
Position = 4,
119119
Mandatory=$True,
120-
ValueFromPipeline=$False,
121120
HelpMessage="Enter a Subject for your email message `nEXAMPLE: Meeting Thursday ")] # End Parameter
122121
[ValidateNotNullOrEmpty()]
123122
[String]$Subject,
@@ -126,49 +125,43 @@ param (
126125
ParameterSetName="Text",
127126
Position = 5,
128127
Mandatory=$True,
129-
ValueFromPipeline=$False,
130128
HelpMessage="Enter a message to place in the body of your email `nEXAMPLE: Hi John, I look forward to our meeting ")] # End Parameter
131129
[String]$Body,
132130

133131
[Parameter(
134132
ParameterSetName="HTML",
135133
Position = 5,
136134
Mandatory=$True,
137-
ValueFromPipeline=$False,
138135
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
139136
[String]$HTMLBody,
140137

141138
[Parameter(
142139
Position = 6,
143140
Mandatory=$False,
144-
ValueFromPipeline=$False,
145141
HelpMessage="Enter the absolute path to a file you to attach to your email send. `nEXAMPLE: C:\temp\file.txt")] # End Parameter
146142
[ValidateScript({Test-Path -Path $_})]
147143
[String[]]$Attachment,
148144

149145
[Parameter(
150146
Position = 7,
151-
Mandatory=$True,
152-
ValueFromPipeline=$False,
147+
Mandatory=$False,
153148
HelpMessage="Enter your SendGrid API key `nEXAMPLE: SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ")] # End Parameter
154149
[ValidateScript({$_.Length -eq 69 -and $_ -like "SG.*"})]
155150
[String]$APIKey
156151
) # End param
157152

153+
$Count = 0
154+
$AllTo = @()
155+
$MailBodyType = 'text/plain'
156+
$MailBodyValue = $Body
157+
158158
If ($PSBoundParameters.ContainsKey("HTMLBody")) {
159159

160160
$MailbodyType = 'text/HTML'
161161
$MailbodyValue = $HTMLBody
162162

163-
} Else {
164-
165-
$MailBodyType = 'text/plain'
166-
$MailBodyValue = $Body
167-
168163
} # End If Else
169164

170-
$Count = 0
171-
$AllTo = @()
172165
ForEach ($T in $ToAddress) {
173166

174167
$AllRecipients = @{}
@@ -232,6 +225,7 @@ param (
232225
"Authorization" = "Bearer $APIKey"
233226
} # End $Header
234227

235-
Invoke-RestMethod -Method POST -Uri https://api.sendgrid.com/v3/mail/send -Headers $Header -Body $BodyJson -ContentType "application/json"
228+
Write-Verbose -Message "[v] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Sending email through SendGrid SMTP server"
229+
Invoke-RestMethod -Method POST -Uri "https://api.sendgrid.com/v3/mail/send" -Headers $Header -Body $BodyJson -ContentType "application/json"
236230

237231
} # End Send-SendGridEmail

0 commit comments

Comments
 (0)