Send mail through SendGrid Web API using PowerShell

Date posted: 2024-11-07
Last updated: 2026-05-12

Learn how to use the Twilio SendGrid Web API v3 to send mail with 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 sending email through SendGrid with PowerShell.

Why use the API over SMTP? Using the SendGrid API is more secure than traditional SMTP, avoids port blocking issues in cloud environments, and provides better delivery analytics. If you are looking for the traditional SMTP method for internal servers, see my basic PowerShell email guide here.

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.

Never hardcode your API key in the script, the above is a mere example. Always use Get-Credential or an environment variable.

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.

Found this guide helpful? You can support my independent deep dives into Windows Server and DevOps by donating via PayPal. Every bit of support helps keep saotn.org fast and updated!

Quick guide: SMTP vs. API

FeatureStandard SMTP (Send-MailMessage)SendGrid API (REST)
Best ForInternal servers, Exchange, local relays.Azure/AWS, Cloud apps, High-volume.
Common Ports587 (StartTLS) or 465 (SSL).443 (HTTPS).
Cloud StatusPort 25 is almost always blocked.Fully supported in all cloud environments.
SecurityBasic Auth / IP Whitelisting.Secure API Keys / Bearer Tokens.
AnalyticsMinimal (Server logs only).Full (Opens, clicks, bounces).

When configuring SMTP, aim for Port 587 with StartTLS as it is the modern standard for secure mail submission. If you are unsure if your server supports it, you can manually test SMTP authentication and StartTLS via telnet or OpenSSL first. Avoid Port 25 for anything other than internal relays, as most modern cloud providers (Azure/AWS) block it by default to prevent spam.

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

  • The article provides a simple PowerShell example to send mail through SendGrid Web API v3, specifically using the /mail/send endpoint.
  • Using the SendGrid API enhances security, avoids SMTP issues, and offers better delivery analytics compared to traditional SMTP.
  • To comply with GDPR, use the Configurable Sending feature to send outbound emails from EU servers by adjusting the API URI.
  • Always secure your API key by using Get-Credential or environment variables instead of hardcoding it in scripts.
  • Twilio SendGrid incorporates robust security options for safe email sending, including TLS, DKIM, and API key management.
Rate this post!

Leave a Comment


Share via
Copy link