Skip to content

Email Namespace

The email namespace provides email service providers.

ts
import { email } from "nevr-env/plugins";

Providers

ProviderDescription
email.resend()Resend - Modern email API

Resend

Modern email API with React Email support, webhooks, and batch sending.

Basic Usage

ts
import { createEnv } from "nevr-env";
import { email } from "nevr-env/plugins";

const env = createEnv({
  plugins: [email.resend()],
  runtimeEnv: process.env,
});

// env.RESEND_API_KEY

Options

OptionTypeDefaultDescription
fromEmailbooleanfalseInclude from email configuration
audiencebooleanfalseInclude audience/contacts config
webhookbooleanfalseInclude webhook secret
domainbooleanfalseInclude custom domain config
replyTobooleanfalseInclude reply-to configuration
batchbooleanfalseInclude batch sending config
variableNamesobject-Custom variable names
extendobject-Extend schema with custom fields

Environment Variables by Option

Default (no options)

VariableRequiredFormatDescription
RESEND_API_KEYre_*Resend API key

fromEmail: true

VariableRequiredFormatDescription
RESEND_FROM_EMAILEmailDefault from email address
RESEND_FROM_NAMEStringDefault from name

audience: true

VariableRequiredDescription
RESEND_AUDIENCE_IDAudience/contacts list ID

webhook: true

VariableRequiredFormatDescription
RESEND_WEBHOOK_SECRETwhsec_*Webhook signing secret

domain: true

VariableRequiredDescription
RESEND_DOMAIN_IDVerified domain ID

replyTo: true

VariableRequiredFormatDescription
RESEND_REPLY_TOEmailDefault reply-to email

batch: true

VariableRequiredDefaultDescription
RESEND_BATCH_SIZE50Batch sending size (1-100)

Examples

ts
// Transactional emails
email.resend({
  fromEmail: true,
})

// With webhooks for tracking
email.resend({
  fromEmail: true,
  webhook: true,
})

// Newsletter/marketing
email.resend({
  fromEmail: true,
  audience: true,
  batch: true,
})

// Full configuration
email.resend({
  fromEmail: true,
  replyTo: true,
  webhook: true,
  domain: true,
})

// Custom templates
email.resend({
  fromEmail: true,
  extend: {
    RESEND_WELCOME_TEMPLATE: z.string().optional(),
    RESEND_RESET_TEMPLATE: z.string().optional(),
    RESEND_INVOICE_TEMPLATE: z.string().optional(),
  }
})

Integration Example

ts
// env.ts
import { createEnv } from "nevr-env";
import { email } from "nevr-env/plugins";

export const env = createEnv({
  plugins: [
    email.resend({
      fromEmail: true,
      webhook: true,
    }),
  ],
  runtimeEnv: process.env,
});

// resend.ts
import { Resend } from "resend";
import { env } from "./env";

export const resend = new Resend(env.RESEND_API_KEY);

// email.ts
import { env } from "./env";
import { resend } from "./resend";
import { WelcomeEmail } from "./emails/welcome";

export async function sendWelcomeEmail(email: string, name: string) {
  await resend.emails.send({
    from: `${env.RESEND_FROM_NAME} <${env.RESEND_FROM_EMAIL}>`,
    to: email,
    subject: "Welcome!",
    react: WelcomeEmail({ name }),
  });
}

// webhook.ts
import { env } from "./env";
import { Webhook } from "svix";

export async function handleWebhook(req: Request) {
  const payload = await req.text();
  const headers = Object.fromEntries(req.headers);
  
  const wh = new Webhook(env.RESEND_WEBHOOK_SECRET);
  const event = wh.verify(payload, headers);
  
  // Handle event (email.sent, email.delivered, etc.)
}

React Email Example

tsx
// emails/welcome.tsx
import {
  Body,
  Container,
  Head,
  Heading,
  Html,
  Preview,
  Text,
} from "@react-email/components";

interface WelcomeEmailProps {
  name: string;
}

export function WelcomeEmail({ name }: WelcomeEmailProps) {
  return (
    <Html>
      <Head />
      <Preview>Welcome to our platform!</Preview>
      <Body>
        <Container>
          <Heading>Welcome, {name}!</Heading>
          <Text>
            Thanks for signing up. We're excited to have you!
          </Text>
        </Container>
      </Body>
    </Html>
  );
}

Released under the MIT License.