Email Service
Better Auth Infrastructure provides a managed transactional email service with pre-built templates for common authentication flows. Send verification emails, password resets, invitations, and more without managing email infrastructure.
Overview
The email service offers:
- Pre-built templates for common authentication flows
- Type-safe template IDs and variables
- Managed delivery
Call sendEmail, sendBulkEmails, and createEmailSender from server code only. They authenticate with your Infrastructure API key.
Installation
The email service is included in @better-auth/infra. Install the package and set BETTER_AUTH_API_KEY as described in Getting Started.
import { sendEmail } from "@better-auth/infra";Quick Start
import { sendEmail } from "@better-auth/infra";
const result = await sendEmail({
template: "verify-email",
to: "user@example.com",
variables: {
verificationUrl: "https://yourapp.com/verify?token=abc123",
userEmail: "user@example.com",
userName: "John",
appName: "Your App",
},
});
if (!result.success) {
console.error("Failed to send email:", result.error);
}These helpers return a result instead of throwing when delivery fails. Check result.success when the caller needs to know whether the email was sent.
To reuse credentials across calls:
import { createEmailSender } from "@better-auth/infra";
const emailSender = createEmailSender({
apiKey: process.env.BETTER_AUTH_API_KEY,
});
await emailSender.send({
template: "reset-password",
to: "user@example.com",
variables: {
resetLink: "https://yourapp.com/reset?token=xyz",
userEmail: "user@example.com",
},
});Use with Better Auth
dash() does not send these emails for you. Pass sendEmail into Better Auth's email callbacks. Keep the callbacks async and await the send:
import { betterAuth } from "better-auth";
import { organization } from "better-auth/plugins";
import { sendEmail } from "@better-auth/infra";
export const auth = betterAuth({
emailAndPassword: {
enabled: true,
async sendResetPassword({ user, url }) {
const result = await sendEmail({
template: "reset-password",
to: user.email,
variables: {
resetLink: url,
userEmail: user.email,
userName: user.name,
appName: "Your App",
},
});
if (!result.success) {
console.error("Failed to send email:", result.error);
}
},
},
emailVerification: {
sendOnSignUp: true,
async sendVerificationEmail({ user, url }) {
const result = await sendEmail({
template: "verify-email",
to: user.email,
variables: {
verificationUrl: url,
userEmail: user.email,
userName: user.name,
appName: "Your App",
},
});
if (!result.success) {
console.error("Failed to send email:", result.error);
}
},
},
plugins: [
organization({
async sendInvitationEmail(data) {
const inviteLink = `https://yourapp.com/accept-invitation/${data.id}`;
const result = await sendEmail({
template: "invitation",
to: data.email,
variables: {
inviteLink,
inviterName: data.inviter.user.name,
inviterEmail: data.inviter.user.email,
organizationName: data.organization.name,
role: data.role,
appName: "Your App",
},
});
if (!result.success) {
console.error("Failed to send email:", result.error);
}
},
}),
],
});On serverless, set advanced.backgroundTasks.handler to your platform's waitUntil. Better Auth then passes this callback promise to the handler on password reset, sign-up verification, and invitations, so the HTTP response does not wait for delivery.
/send-verification-email awaits sendVerificationEmail and does not use backgroundTasks. That request waits for delivery. Do not void the send in any of these callbacks: Better Auth can only track a promise the callback returns.
Available Templates
verify-email
Sends an email verification link to new users.
await sendEmail({
template: "verify-email",
to: "user@example.com",
variables: {
verificationUrl: "https://yourapp.com/verify?token=abc",
userEmail: "user@example.com",
verificationCode: "123456", // Optional: for code-based verification
userName: "John", // Optional
appName: "Your App", // Optional
expirationMinutes: "60", // Optional
},
});reset-password
Sends a password reset link.
await sendEmail({
template: "reset-password",
to: "user@example.com",
variables: {
resetLink: "https://yourapp.com/reset?token=xyz",
userEmail: "user@example.com",
userName: "John", // Optional
appName: "Your App", // Optional
expirationMinutes: "60", // Optional
},
});change-email
Confirms an email address change request.
await sendEmail({
template: "change-email",
to: "newemail@example.com",
variables: {
confirmationLink: "https://yourapp.com/confirm-email?token=abc",
newEmail: "newemail@example.com",
currentEmail: "oldemail@example.com",
userName: "John", // Optional
appName: "Your App", // Optional
expirationMinutes: "60", // Optional
},
});sign-in-otp
Sends a one-time password for passwordless sign-in.
await sendEmail({
template: "sign-in-otp",
to: "user@example.com",
variables: {
otpCode: "123456",
userEmail: "user@example.com",
appName: "Your App", // Optional
expirationMinutes: "10", // Optional
},
});verify-email-otp
Sends an OTP code for email verification.
await sendEmail({
template: "verify-email-otp",
to: "user@example.com",
variables: {
otpCode: "123456",
userEmail: "user@example.com",
appName: "Your App", // Optional
expirationMinutes: "10", // Optional
},
});reset-password-otp
Sends an OTP code for password reset.
await sendEmail({
template: "reset-password-otp",
to: "user@example.com",
variables: {
otpCode: "123456",
userEmail: "user@example.com",
appName: "Your App", // Optional
expirationMinutes: "10", // Optional
},
});magic-link
Sends a magic link for passwordless authentication.
await sendEmail({
template: "magic-link",
to: "user@example.com",
variables: {
magicLink: "https://yourapp.com/auth/magic?token=abc",
userEmail: "user@example.com",
appName: "Your App", // Optional
expirationMinutes: "15", // Optional
},
});two-factor
Sends a two-factor authentication code.
await sendEmail({
template: "two-factor",
to: "user@example.com",
variables: {
otpCode: "123456",
userEmail: "user@example.com",
userName: "John", // Optional
appName: "Your App", // Optional
expirationMinutes: "5", // Optional
},
});invitation
Sends an organization invitation.
await sendEmail({
template: "invitation",
to: "newmember@example.com",
variables: {
inviteLink: "https://yourapp.com/invite?token=abc",
inviterName: "John Smith",
inviterEmail: "john@company.com",
organizationName: "Acme Corp",
role: "Member",
appName: "Your App", // Optional
expirationDays: "7", // Optional
},
});application-invite
Sends an application-level invitation (inviting users to the platform).
await sendEmail({
template: "application-invite",
to: "newuser@example.com",
variables: {
inviteLink: "https://yourapp.com/join?token=abc",
inviterName: "John Smith",
inviterEmail: "john@company.com",
inviteeEmail: "newuser@example.com",
appName: "Your App", // Optional
expirationDays: "7", // Optional
},
});delete-account
Sends account deletion confirmation.
await sendEmail({
template: "delete-account",
to: "user@example.com",
variables: {
deletionLink: "https://yourapp.com/confirm-delete?token=abc",
userEmail: "user@example.com",
userName: "John", // Optional
appName: "Your App", // Optional
expirationMinutes: "60", // Optional
},
});stale-account-user
Notifies a user that their dormant account was accessed.
await sendEmail({
template: "stale-account-user",
to: "user@example.com",
variables: {
userEmail: "user@example.com",
daysSinceLastActive: "90",
loginTime: "February 20, 2026, 3:45 PM UTC",
userName: "John", // Optional
appName: "Your App", // Optional
loginLocation: "New York, US", // Optional
loginDevice: "Chrome on Windows", // Optional
loginIp: "192.168.1.1", // Optional
},
});stale-account-admin
Notifies an admin about dormant account reactivation.
await sendEmail({
template: "stale-account-admin",
to: "admin@yourapp.com",
variables: {
userEmail: "user@example.com",
userId: "user_123",
adminEmail: "admin@yourapp.com",
daysSinceLastActive: "90",
loginTime: "February 20, 2026, 3:45 PM UTC",
userName: "John", // Optional
appName: "Your App", // Optional
loginLocation: "New York, US", // Optional
loginDevice: "Chrome on Windows", // Optional
loginIp: "192.168.1.1", // Optional
},
});Configuration
| Option | Type | Description |
|---|---|---|
apiKey | string | Your API key. Falls back to BETTER_AUTH_API_KEY. |
apiUrl | string | Better Auth Infrastructure API URL. Default: https://dash.better-auth.com |
apiOptions | object | HTTP client options. Accepts timeout?: number (ms, default 3000) and headers?: Record<string, string>. |
apiTimeout | number | Deprecated alias for apiOptions.timeout. |
Pass this object to createEmailSender, or as the second argument to sendEmail / sendBulkEmails. If you omit apiKey or apiUrl, the client reads BETTER_AUTH_API_KEY and BETTER_AUTH_API_URL.
API Reference
sendEmail
async function sendEmail<T extends EmailTemplateId>(
options: SendEmailOptions<T>,
config?: EmailConfig
): Promise<SendEmailResult>| Property | Type | Required | Description |
|---|---|---|---|
template | EmailTemplateId | Yes | Template to send. Variables are typed from this ID. |
to | string | Yes | Recipient email address. |
variables | EmailTemplateVariables<T> | Yes | Template variables. Required and optional fields depend on template. |
subject | string | No | Subject override. Uses the template default when omitted. |
clientIp | string | No | End-user IP for abuse limits. Omit in Better Auth callbacks. If you send outside a request, pass only an IP your server determined — never a client-supplied value. |
createEmailSender
const emailSender = createEmailSender(config?: EmailConfig);
await emailSender.send(options);
await emailSender.sendBulk(options);
const templates = await emailSender.getTemplates();getTemplates() lists templates available to your project. It returns an empty array if the request fails.
sendBulkEmails
async function sendBulkEmails<T extends EmailTemplateId>(
options: SendBulkEmailsOptions<T>,
config?: EmailConfig
): Promise<SendBulkEmailsResult>const result = await sendBulkEmails({
template: "reset-password",
emails: [
{
to: "user1@example.com",
variables: {
resetLink: "https://yourapp.com/reset?token=one",
userEmail: "user1@example.com",
},
},
{
to: "user2@example.com",
variables: {
resetLink: "https://yourapp.com/reset?token=two",
userEmail: "user2@example.com",
},
},
],
});| Property | Type | Required | Description |
|---|---|---|---|
template | EmailTemplateId | Yes | Template used for every email. |
emails | Array<{ to: string; variables?: EmailTemplateVariables<T> }> | Yes | Recipients. Per-recipient variables override shared variables. |
subject | string | No | Subject override shared across all emails. |
variables | Record<string, string> | No | Shared variables applied to every email. |
clientIp | string | No | Same rules as sendEmail. |
Results
interface SendEmailResult {
success: boolean;
messageId?: string;
error?: string;
}
interface SendBulkEmailsResult {
success: boolean;
failures?: Record<string, { error?: string; messageId?: string }[]>;
}Treat error as an opaque string. Bulk failures is keyed by recipient address.
Plan Requirements
| Feature | Starter | Pro | Business | Enterprise |
|---|---|---|---|---|
| Transactional Email | - | Yes | Yes | Yes |
Transactional email is available on Pro plans and above.
Security Plugin (sentinel)
The `sentinel()` plugin provides comprehensive security and abuse protection for your authentication system. It detects and prevents various attack vectors including credential stuffing, impossible travel, free trial abuse, and more.
SMS Service
Better Auth Infrastructure provides a managed SMS service for sending OTP codes for phone verification and two-factor authentication. Send verification codes without managing SMS providers.