0

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
  • 1
    As aside: looking at your previous question, why on earth did you remove all indentation from the code?? This makes things extremely hard to read. Also, `Send-SendGridEmail u/splat2` should be `Send-SendGridEmail @splat2`. To send to more than one recipient, change parameter `[String] $destEmailAddress` into `[String[]] $destEmailAddress` and add an array of recipients in $splat2: `destEmailAddress = 'you@domain.com','someoneelse@domain.com',ýetanother@domain.com'` – Theo Aug 24 '21 at 17:56
  • sorry about that. I will fix it. I posed the same question in reddit so i took from there for more coverage. Also it is @, i don't know why it became u/ – Milan Patel Aug 24 '21 at 17:58
  • The following didn work: `[String[]] $destEmailAddress` `destEmailAddress = 'you@domain.com','someoneelse@domain.com',ýetanother@domain.com' ` – Milan Patel Aug 24 '21 at 18:07
  • You're missing a single quote. – Abraham Zinala Aug 24 '21 at 18:18
  • a single quote around the whole thing? like where? I tested with two emails I have: `'firstmail@domain.com','secondmail@domain.com'` – Milan Patel Aug 24 '21 at 18:19
  • Aha, that happened because my US International Keyboard setting combined the single-quote with the `y`. Should have been `'you@domain.com','someoneelse@domain.com','yetanother@domain.com'`.. Anyway, these were just examples after all.. – Theo Aug 24 '21 at 18:21
  • of course =). I didn't put my actual intended emails. They are valid emails because they are 2 of my own. I test sending to myself to confirm. – Milan Patel Aug 24 '21 at 19:54

0 Answers0