0

I'm using this as part of the runbook in Azure. The email works fine if I'm only using the To field for emails

$jsonRequest = [ordered]@{
personalizations= @(@{to = @(@{email = "$To"})
subject = "$SubJect" })

                        from = @{email = "$From"}
                        content = @( @{ type = "text/HTML"
                                    value = "$Body" }
                        )} | ConvertTo-Json -Depth 10

Invoke-RestMethod -Uri "https://api.sendgrid.com/v3/mail/send" -Method Post -Headers $headers -Body $jsonRequest

However if I'm adding the CC field, then I get 400 Bad request error. Is there another way to incorporate CC into the same request?

$jsonRequest = [ordered]@{
                        personalizations= @(@{to = @(@{email =  "$To"})
                            cc = @{email = "$cc"}
                            subject = "$SubJect" })
                            
                            from = @{email = "$From"}
                            content = @( @{ type = "text/HTML"
                                        value = "$Body" }
                            )} | ConvertTo-Json -Depth 10
Invoke-RestMethod   -Uri "https://api.sendgrid.com/v3/mail/send" -Method Post -Headers $headers -Body $jsonRequest 
WinXu
  • 1
  • You can refer to [Azure runbook script to send mail using PowerShell and SendGrid](https://www.sharepointwidgets.com/2021/01/azure-runbook-script-to-send-mail-using.html) and [Django: adding cc in email with sengrid returns HTTP Error 400: Bad Request](https://stackoverflow.com/questions/66282325/django-adding-cc-in-email-with-sengrid-returns-http-error-400-bad-request) – Ecstasy Feb 04 '22 at 06:49
  • Hi DeepDave-MT, thank you so much for providing the links. We have tried the script from the first link but still getting the 400 Bad request error. It looks like SendGrid gives the error when we have both $to and $cc in the personalization part. – WinXu Feb 07 '22 at 21:43

1 Answers1

0

You have to declare the variable that handles the 'cc' in the parameters part. This worked for me:

function Send-EmailWithSendGrid {
Param
    (
        [Parameter(Mandatory=$true)]
        [string] $From,
 
        [Parameter(Mandatory=$true)]
        [String] $To,

        [Parameter(Mandatory=$true)]
        [String] $cc,

        [Parameter(Mandatory=$true)]
        [string] $ApiKey,

        [Parameter(Mandatory=$true)]
        [string] $Subject,

        [Parameter(Mandatory=$true)]
        [string] $Body

    )

$headers = @{}
$headers.Add("Authorization","Bearer $apiKey")
$headers.Add("Content-Type", "application/json")
$jsonRequest = [ordered]@{personalizations= @(@{to = @(@{email =  "$To"})
                            cc = @(@{email =  "$cc"})
                                subject = "$SubJect" })
                                from = @{email = "$From"}
                                content = @( @{ type = "text/html"
                                            value = "$Body" }

                                )} | ConvertTo-Json -Depth 10
    Invoke-RestMethod   -Uri "https://api.sendgrid.com/v3/mail/send" -Method Post -Headers $headers -Body $jsonRequest 
    }
$From = "youremail@domain.com"
$To = "abc@gmail.com"
$cc = "def@gmail.com"
$APIKEY = "YouAPIkey"

$Subject = "Test"
$Body = "Body here"

                   
Send-EmailWithSendGrid -from $from -to $To -cc $cc -ApiKey $APIKEY -Body $Body -Subject $Subject
          
Luxo
  • 1