Prompt
How do I send email from a serverless app?
Latest observation
Nov 13, 2026
OpenAI APIWeb search: off
To send email from a serverless app, the usual approach is:
-
Use a third-party email service
Examples: SendGrid, Mailgun, Amazon SES, Postmark, Resend, etc. -
Call that service from a serverless function
Your app (e.g. AWS Lambda, Vercel Functions, Azure Functions, Cloudflare Workers) invokes the email API when needed. -
Keep credentials in environment variables / secrets
Never hardcode API keys in client-side code.
Common pattern
- User submits a form
- Your frontend sends the data to a serverless endpoint
- The function validates input
- The function sends the email through an email provider API
- Return success/failure to the frontend
Example using Node.js + SendGrid
import sgMail from '@sendgrid/mail';
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const { name, email, message } = req.body;
try {
await sgMail.send({
to: 'you@example.com',
from: 'verified-sender@yourdomain.com',
subject: `New message from ${name}`,
text: `From: ${email}\n\n${message}`,
});
return res.status(200).json({ ok: true });
} catch (err) {
console.error(err);
return res.status(500).json({ error: 'Failed to send email' });
}
}
Things to watch out for
- Domain verification: Most services require you to verify your sender domain.
- Spam prevention: Add CAPTCHA, rate limiting, and input validation.
- Cold starts / timeouts: Keep the function simple and fast.
- Deliverability: Set up SPF, DKIM, and DMARC for your domain.
- Never send directly from the browser: That exposes your email API key.
If you want the simplest setup
Use a provider with a straightforward API:
- Resend: very simple for modern apps
- Postmark: great deliverability
- SendGrid: popular, lots of features
- Amazon SES: cheaper at scale, more setup
If you tell me your serverless platform (AWS Lambda, Vercel, Netlify, Cloudflare Workers, etc.) and your language, I can give you a ready-to-use example.