# Email

Send email from your server through Resend or Cloudflare, and write messages as Markdown templates.

Hosts: Node, Docker, Cloudflare, Vercel, Netlify, Deno.

:hero-figure{name="email"}

```ts [server/api/welcome.post.ts]
import { defineEventHandler } from 'h3'
import { email } from 'vite-hub/email'

export default defineEventHandler(async () => {
  // Render server/emails/welcome.md. TypeScript checks `data` against the template.
  const [error, sent] = await email.send({
    template: 'welcome',
    data: { user: { name: 'Ada' } },
    from: 'verified-sender@example.com',
    to: 'ada@example.com',
    subject: 'Your workspace is ready',
  })
  if (error) throw error
  return sent
})
```

```md [server/emails/welcome.md]
# Welcome {{ data.user.name }}

Your workspace is ready.
```

:::framework{name="Nuxt"}
```ts [nuxt.config.ts]
import { env } from 'vite-hub/env'

export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'node',
    // Resend sends the email
    email: {
      driver: 'resend',
      // The server reads the key when it sends
      options: {
        apiKey: env({
          secret: true,
          source: env.source('RESEND_API_KEY'),
        }),
      },
    },
  },
})
```
:::
:::framework{name="Nitro"}
```ts [vite.config.ts]
import { vitehub } from 'vite-hub'
import { env } from 'vite-hub/env'
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'

export default defineConfig({
  plugins: [vitehub({
    host: 'node',
    // Resend sends the email
    email: {
      driver: 'resend',
      // The server reads the key when it sends
      options: {
        apiKey: env({
          secret: true,
          source: env.source('RESEND_API_KEY'),
        }),
      },
    },
  }), nitro()],
})
```
:::
:::framework{name="Vite"}
```ts [vite.config.ts]
import { vitehub } from 'vite-hub'
import { env } from 'vite-hub/env'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [vitehub({
    host: 'node',
    // Resend sends the email
    email: {
      driver: 'resend',
      // The server reads the key when it sends
      options: {
        apiKey: env({
          secret: true,
          source: env.source('RESEND_API_KEY'),
        }),
      },
    },
  })],
})
```
:::

```ts [test/welcome.test.ts]
import { expect, it } from 'vitest'
import { createTestEmail } from 'vite-hub/email/test'

// Check an email without sending it
it('sends the welcome email', async () => {
  const mail = createTestEmail()
  await mail.send({
    from: 'hello@example.com',
    to: 'you@example.com',
    subject: 'Welcome',
    text: 'Hello',
  })
  expect(mail.messages[0]?.subject).toBe('Welcome')
})
```

### [Pick a provider in one place](https://vitehub.dev/docs/email/configure#configure-vite)

Pick Resend or Cloudflare Email in your Vite config. Your routes stay the same.

### [Keys stay out of your code](https://vitehub.dev/docs/email/get-started#provide-the-resend-secret)

The server reads the API key from an environment variable when it sends.

### [Write emails in Markdown](https://vitehub.dev/docs/email/server-api#send-a-template)

Files in `server/emails` become typed templates. `email.send({ template, data })` renders HTML and text.

### [See each email while you develop](https://vitehub.dev/docs/email/hosts#development-outbox)

In `vite dev`, each email stays in an outbox. `remote: true` also sends it through the provider.

### [Test without sending](https://vitehub.dev/docs/email/server-api#test-without-delivery)

`createTestEmail()` keeps messages in memory for your tests to check.

### [Clear errors, no double sends](https://vitehub.dev/docs/email/limits-and-errors#handle-delivery-errors)

Each failure has an `EMAIL_*` code. Email does not retry, so nothing sends twice by mistake.

Use Email for outbound transactional messages. Email keeps delivery behind a provider-neutral driver and supports Markdown templates with a text fallback.

Keep recipients, sender identity, and delivery authorization in application policy. Use test capture or a local driver before sending to real recipients.

Prove rendered HTML and text output, a rejected address, and the provider response. Do not claim delivery from a queued request alone.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/email.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/email/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/email/configure.md)
- [Server API](https://vitehub.dev/raw/docs/email/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/email/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/email/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/email/limits-and-errors.md)

Next: [EMAIL_AUTHENTICATION](https://vitehub.dev/raw/docs/email/diagnostics/email_authentication.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
