Send Email from Express
Deploy email infrastructure and send your first email from an Express application.
Deploy email infrastructure and send your first email from an Express application.
Before you begin, make sure you have:
Run the Wraps CLI to deploy email infrastructure to your AWS account:
npx @wraps.dev/cli email initAdd and verify your sending domain with AWS SES:
npx @wraps.dev/cli email domains add -d yourdomain.comDNS Setup
The CLI will output DKIM records to add to your DNS provider. Once added, verify them with npx @wraps.dev/cli email domains verify -d yourdomain.com
Install the @wraps.dev/email package in your Express project:
npm install @wraps.dev/emailCreate a singleton email service that reuses AWS connections across requests:
import { WrapsEmail } from '@wraps.dev/email';// Create a singleton instance — reuses AWS connectionsexport const email = new WrapsEmail({ region: process.env.AWS_REGION || 'us-east-1',});Create an Express route that accepts email parameters and sends using the SDK:
import { Router } from 'express';import { email } from '../services/email';const router = Router();router.post('/send', async (req, res, next) => { try { const { to, subject, html } = req.body; const result = await email.send({ from: 'hello@yourdomain.com', to, subject, html, }); res.json({ success: true, messageId: result.messageId }); } catch (error) { next(error); }});export default router;Add error handling middleware to return meaningful error responses for email failures:
import { SESError, ValidationError } from '@wraps.dev/email';export function emailErrorHandler(err, req, res, next) { if (err instanceof ValidationError) { return res.status(400).json({ error: err.message, field: err.field }); } if (err instanceof SESError) { const status = err.retryable ? 503 : 400; return res.status(status).json({ error: err.message, code: err.code }); } next(err);}Error Types
Learn about all available methods, options, and advanced features.
View SDK DocsReference for all error codes and troubleshooting steps.
View Errors