Prompt
How do I send email through an API instead of SMTP?
Latest observation
Nov 6, 2026
OpenAI APIWeb search: off
You can send email through an API by using an email service provider’s HTTP API instead of connecting to an SMTP server. The basic flow is:
-
Choose an email API provider
- Common options: SendGrid, Mailgun, Amazon SES, Postmark, Brevo, etc.
- They usually give you:
- an API key
- a REST endpoint
- documentation and SDKs
-
Authenticate with an API key
- You send the key in a request header or via an SDK.
- Example header:
Authorization: Bearer YOUR_API_KEY
-
Make an HTTP request to send mail
- Usually a
POSTrequest with JSON payload containing:fromtosubjecttextorhtml- optional attachments, cc/bcc, reply-to, etc.
- Usually a
-
Handle the response
- A successful request often returns a message ID or status.
- You should also handle errors like invalid recipient, auth failure, rate limits, or blocked content.
Example using raw curl with a generic API shape:
curl -X POST https://api.emailprovider.com/v1/send \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"from": "you@example.com",
"to": "recipient@example.com",
"subject": "Hello",
"text": "This was sent via API"
}'
Example in Node.js with fetch:
const res = await fetch("https://api.emailprovider.com/v1/send", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.EMAIL_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
from: "you@example.com",
to: "recipient@example.com",
subject: "Hello",
text: "This was sent via API"
})
});
const data = await res.json();
console.log(data);
Why use an API instead of SMTP?
- Easier authentication
- Better deliverability tools
- Built-in templates and analytics
- Faster integration in web/mobile apps
- Less fragile than managing SMTP connections
Important notes
- Verify your domain with the provider to avoid spam/filtering issues.
- Never expose your API key in frontend code; send email from your backend only.
- Many providers support template-based emails, event webhooks, and suppression lists.
If you want, I can give you:
- a working example for a specific provider (SendGrid, Mailgun, SES, Postmark, etc.), or
- code in Python, Node.js, PHP, Go, or Java.