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 email using PowerShell.
In this article
SendGrid v3 /mail/send: UTF-8 from PowerShell: PowerShell 5.1 encodes the body as ISO-8859-1, SendGrid picks the body charset itself, If the body really has to be UTF-8, Pitfall: encoding of the script file
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 SendGrid email 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 email 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.
Something I found out the hard way: special characters in mails via SendGrid. And PowerShell for that matter. See below, Claude.ai assisted me with debugging and testing.
SendGrid v3 /mail/send: UTF-8 from PowerShell
Mails we send through the SendGridv3 API (/v3/mail/send) used an older character set instead of the standard UTF-8. They arrived with Content-Type: text/html; charset=iso-8859-1. As a result, characters such as ë and é were at risk of displaying incorrectly for the recipient, for example patiënten instead of the Dutch word patiënten. There turned out to be two separate causes.
PowerShell 5.1 encodes the body as ISO-8859-1
This was the actual bug. In Windows PowerShell 5.1, Invoke-RestMethod encodes a string -Body as ISO-8859-1 when -ContentType has no charset. SendGrid expects UTF-8 and received Latin-1 bytes instead.
Original call:
Invoke-RestMethod -Method POST `
-Uri https://api.eu.sendgrid.com/v3/mail/send `
-Headers $headers `
-ContentType 'application/json' `
-Body $bodyParams
Fixed call:
Invoke-RestMethod -Method POST `
-Uri https://api.eu.sendgrid.com/v3/mail/send `
-Headers $headers `
-ContentType 'application/json; charset=utf-8' `
-Body ([System.Text.Encoding]::UTF8.GetBytes($bodyParams))
- The
byte[]body does the real work. PowerShell no longer encodes anything itself. charset=utf-8in theContentTypeis an extra safeguard.- In PowerShell 7.4+ UTF-8 is the default, so the problem does not occur there. The call above works in both versions.
Verification: the subject of the received mail is now correctly UTF-8 encoded, Subject: Hello, =?UTF-8?B?cGF0acOrbnRlbg==?=. C3 AB is ë in UTF-8.
SendGrid picks the body charset itself
Even after the fix, the body stayed charset=iso-8859-1. That is not a bug. SendGrid picks the smallest charset that can hold every character in the body:
| Content | Resulting body charset |
|---|---|
Only Latin-1 characters in body (ë) | charset=iso-8859-1, =EB |
Character outside Latin-1 in body (€) | charset=utf-8 |
| Character outside Latin-1 in subject only | charset=iso-8859-1 |
All three were tested by hand, using the message source in Outlook. The subject is encoded separately and does not affect the body charset. iso-8859-1 with quoted-printable is a valid encoding, mail clients display the text correctly.
Setting a charset in the type field of content (text/html; charset=utf-8) is not documented and has not been tested. The Content-Type header itself is reserved in v3 and cannot be set through the headers object.
If the body really has to be UTF-8
This is only needed when a downstream system relies on the label.
- HTML entities, for example
patiëntenorë. The body is then pure ASCII and the charset no longer matters. This is the clean solution. - JSON escapes, for example
"patiënten". Makes the request itself ASCII-only. Handy for short fixed texts. - A character outside Latin-1 in the body, such as a zero-width space (U+200B). Forces UTF-8, but it is a hack.
Pitfall: encoding of the script file
Are accented characters written as literals in a .ps1 file saved as UTF-8 without BOM? Then PowerShell 5.1 reads the file as Windows-1252, and patiënten already becomes patiënten while parsing. Save scripts with such literals as UTF-8 with BOM, or read the text with Get-Content -Encoding UTF8 -Raw.
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.
Quick guide: SMTP vs. API
| Feature | Standard SMTP (Send-MailMessage) | SendGrid API (REST) |
| Best For | Internal servers, Exchange, local relays. | Azure/AWS, Cloud apps, High-volume. |
| Common Ports | 587 (StartTLS) or 465 (SSL). | 443 (HTTPS). |
| Cloud Status | Port 25 is almost always blocked. | Fully supported in all cloud environments. |
| Security | Basic Auth / IP Whitelisting. | Secure API Keys / Bearer Tokens. |
| Analytics | Minimal (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 email 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.
This post was most recently updated on .
