Send mail through SendGrid API using PowerShell

Learn how to use the Twilio SendGrid Web API v3 to send mail with PowerShell
Published on Thursday, 7 November 2024

I found the SendGrid API documentation is lacking an simple PowerShell example to use their API, so here it is. Use Twilio SendGrid Web API v3 endpoints, including the new v3 /mail/send.

# fill in your API key
$apikey = ''

$headers = @{
  Authorization="Bearer ${apikey}"
}

$bodyParams = @'
  {
    "personalizations": [
      {
        "to": [
          {
            "email": "recipient@example.net",
            "name": "Firstname Lastname"
          }
        ],
        "subject": "Hello, World!"
      }
    ],
    "content": [
      {
        "type": "text/plain",
        "value": "Heya!"
      }
    ],
    "from": {
      "email": "sender@example.com",
      "name": "Firstname Lastname"
    },
    "reply_to": {
      "email": "sender@example.net",
      "name": "First Name"
    }
  }
'@

Invoke-RestMethod -Method POST `
  -Uri https://api.sendgrid.com/v3/mail/send `
  -Headers $headers `
  -ContentType 'application/json' `
  -Body $bodyParams

This is it. If you need to use the EU region, verify your API key is valid and change the URI to api.eu.sendgrid.com.

See for more examples you can port to PowerShell the API documentation: