Prompt
How do I send emails from a serverless function?
Latest observation
Sep 23, 2026
OpenAI APIWeb search: off
There are a few common ways to send email from a serverless function, and the best one depends on whether you want to send transactional email (password resets, receipts, alerts) or just need a simple SMTP setup.
Recommended approach: use an email API
For serverless functions, the easiest and most reliable method is usually to call a third-party email service API such as:
- SendGrid
- Mailgun
- Postmark
- Amazon SES
- Resend
Why this is best for serverless
- No need to manage your own mail server
- Works well with short-lived functions
- Better deliverability than raw SMTP in many cases
- Easier to configure with environment variables
- Supports retries, templates, logs, and webhooks
Basic pattern
Your serverless function:
- Receives a request
- Validates the input
- Calls the email provider’s API
- Returns success/failure
Example: Node.js using SendGrid
import sgMail from '@sendgrid/mail';
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
export default async function handler(req, res) {
try {
const { to, subject, text } = req.body;
await sgMail.send({
to,
from: 'you@yourdomain.com',
subject,
text,
});
res.status(200).json({ message: 'Email sent' });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to send email' });
}
}
Set your secret in environment variables:
SENDGRID_API_KEY
Alternative: SMTP
You can also send mail with SMTP using libraries like nodemailer.
Example: Node.js with Nodemailer
import nodemailer from 'nodemailer';
export default async function handler(req, res) {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: 587,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});
try {
await transporter.sendMail({
from: 'you@yourdomain.com',
to: req.body.to,
subject: req.body.subject,
text: req.body.text,
});
res.status(200).json({ message: 'Email sent' });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Email failed' });
}
}
Caveats with SMTP in serverless
- Cold starts can slow connection setup
- Some providers block SMTP or require extra configuration
- Connection reuse is limited in serverless environments
- More likely to hit timeouts if the SMTP server is slow
Best practices
- Store credentials in environment variables, not code
- Verify sender domain with SPF/DKIM/DMARC
- Validate recipient addresses and input
- Use rate limiting to prevent abuse
- Keep function execution short
- Log provider errors for debugging
- Use templates for consistent formatting
If you’re using a specific platform
- AWS Lambda: SES is a strong choice
- Vercel / Netlify / Cloudflare Workers: API-based providers are usually easiest
- Firebase Functions: SendGrid, Mailgun, or SES all work well
If you want, I can give you a copy-paste example for your specific serverless platform (AWS Lambda, Vercel, Netlify, Cloudflare Workers, etc.) and language.