Send mail through SendGrid Web API using PowerShell

Home » Codebase » Send mail through SendGrid Web API using PowerShell

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 to send mail using PowerShell.

The SendGrid v3 Web API provides a REST-like interface that enables you to send email at scale, ensuring your application can handle high-volume email with ease. Learn to use PowerShell to send mails through SendGrid.

Learn how to use the Twilio SendGrid Web API v3 to send mail with PowerShell without additional modules.

# 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.

Configurable Sending in the EU

This feature empowers you to send outbound emails directly from servers located within the European Union, ensuring compliance with GDPR regulations and enhancing data privacy. If you need to use the EU region, verify your API key is valid and change the URI to api.eu.sendgrid.com.

Conclusion

Twilio SendGrid is a company having their security options in place for secure mail sending. With security options you have to think about SPF, DNSSEC, StartTLS, encryption, authentication and account protection features. These include opportunistic TLS, enforced TLS, DKIM, SPF, DMARC, API keys with granular permissions, two-factor authentication, and IP Access Management.

Being able to send an email through SendGrid, using either PowerShell or .NET, can come in handy when sending DevOps reporting mails. This is a nice addition to my Send email with PowerShell post, but without additional modules.

Summary

  • This article provides a simple PowerShell example to send mail through the SendGrid Web API using PowerShell.
  • The SendGrid v3 Web API enables high-volume email sending with a REST-like interface.
  • You can configure sending from servers in the EU to comply with GDPR by using the api.eu.sendgrid.com URI.
  • Twilio SendGrid offers various security features for secure email sending, including TLS, DKIM, and two-factor authentication.
  • Sending emails through SendGrid using PowerShell is beneficial for tasks like DevOps reporting without needing additional modules.
Jan Reilink
Jan Reilink

In my day to day work, I’m a systems administrator – DevOps / SRE and applications manager at Embrace – The Human Cloud. At Embrace we develop, maintain and host social intranets for our clients. Provide digital services and make working more efficient within various sectors.

Want to support me and donate? Use this link: https://www.paypal.com/paypalme/jreilink.

Articles: 163