I had a previous issue where sending an excel file wouldn't work because it got corrupted but got that working thanks to some help. Now a new issue I have is that I don't know how to send to multiple recipients. The main issue is that my sendgrid code is within a loop that reads from a file, and the number of recipients is dynamic. And I get the structure of the code from an existing github source. MAINLY: Need to know how to send to multiple recipients via variable using the structure below.
function Send-SendGridEmail {
param(
[Parameter(Mandatory = $true)]
[String] $destEmailAddress,
[Parameter(Mandatory = $true)]
[String] $fromEmailAddress,
[Parameter(Mandatory = $true)]
[String] $subject,
[Parameter(Mandatory = $false)]
[string]$contentType = 'text/plain',
[Parameter(Mandatory = $true)]
[String] $contentBody,
[parameter(Mandatory = $false)]
[string]$FileName,
[parameter(Mandatory = $false)]
[string]$FileNameWithFilePath,
[parameter(Mandatory = $false)]
[string]$AttachementType
)
<#
.Synopsis
Function to send email with SendGrid
.Description
A function to send a text or HTML based email
See https://sendgrid.com/docs/API_Reference/api_v3.html for API details
This script provided as-is with no warranty. Test it before you trust it.
www.ciraltos.com
.Parameter apiKey
The SendGrid API key associated with your account
.Parameter destEmailAddress
The destination email address
.Parameter fromEmailAddress
The from email address
.Parameter subject
Email subject
.Parameter type
The content type, values are “text/plain” or “text/html”. "text/plain" set by default
.Parameter content
The content that you'd like to send
.Example
Send-SendGridEmail
#>
############ Update with your SendGrid API Key ####################
$apiKey = "key"
$headers = @{
'Authorization' = 'Bearer ' + $apiKey
'Content-Type' = 'application/json'
}
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($FileNameWithFilePath))
$body = @{
personalizations = @(
@{
to = @(
@{
email = $destEmailAddress
}
)
}
)
from = @{
email = $fromEmailAddress
}
subject = $subject
content = @(
@{
type = $contentType
value = $contentBody
}
)
attachments = @(
@{
content=$base64string
filename=$FileName
type= $AttachementType
disposition="attachment"
}
)
}
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
}
}
# Call the function
# Splat the input
#Send-SendGridEmail @splat
# Sample code with HTML body
$htmlBody = @"
//stuff
"@
$splat2 = @{
destEmailAddress = '""
fromEmailAddress = 'noreply@domaint.com'
subject = 'Test Email'
contentType = 'text/html'
contentBody = $htmlBody
FileName = "filename"
FileNameWithFilePath = "path\excel.xlsx"
AttachementType = "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet"
}
Send-SendGridEmail @splat2