# Send Email from Next.js

Deploy email infrastructure and send your first email from a Next.js application.

## Prerequisites

- Node.js 18 or later
- AWS credentials configured ([AWS Setup Guide](https://wraps.dev/docs/guides/aws-setup))
- A domain you own

## Step 1: Deploy Infrastructure

```bash
npx @wraps.dev/cli email init
```

Deploys SES, DynamoDB, Lambda, EventBridge, and IAM roles. Takes 1-2 minutes.

## Step 2: Add Your Domain

```bash
npx @wraps.dev/cli email domains add -d yourdomain.com
```

The CLI outputs DKIM CNAME records to add to your DNS provider. Verify after adding:

```bash
npx @wraps.dev/cli email domains verify -d yourdomain.com
```

## Step 3: Install the SDK

```bash
npm install @wraps.dev/email
```

## Step 4: Send from a Server Action

```typescript
// app/actions/send-email.ts
'use server'

import { WrapsEmail } from '@wraps.dev/email';

const email = new WrapsEmail();

export async function sendWelcomeEmail(to: string, name: string) {
  const result = await email.send({
    from: 'hello@yourdomain.com',
    to,
    subject: `Welcome, ${name}!`,
    html: `<h1>Welcome to our app, ${name}!</h1><p>We're glad you're here.</p>`,
  });

  return { messageId: result.messageId };
}
```

## Step 5: Send from an API Route

```typescript
// app/api/send/route.ts
import { NextResponse } from 'next/server';
import { WrapsEmail } from '@wraps.dev/email';

const email = new WrapsEmail();

export async function POST(request: Request) {
  const { to, subject, html } = await request.json();

  const result = await email.send({
    from: 'hello@yourdomain.com',
    to,
    subject,
    html,
  });

  return NextResponse.json({ messageId: result.messageId });
}
```

## Step 6: Deploy to Vercel

When deploying to Vercel, Wraps uses OIDC for authentication — no AWS access keys needed. Set `AWS_ROLE_ARN` in your Vercel environment variables.

## Next Steps

- [Email SDK Reference](https://wraps.dev/docs/sdk-reference)
- [Templates Guide](https://wraps.dev/docs/guides/templates)
- [Vercel Setup Guide](https://wraps.dev/docs/guides/vercel-setup)
