Prompt

How do I send emails from a serverless function?

Technology / Api platforms1 observationLast seen Sep 23, 2026

Latest observation

Sep 23, 2026OpenAI 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:

  1. Receives a request
  2. Validates the input
  3. Calls the email provider’s API
  4. 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.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as Circuit. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.