Skip to main content

Sending email

Email verification, password reset and email change send links by email. Give NextBase a mail option with any provider, such as Resend, SES, Postmark or SMTP:

lib/nextbase.ts
import { NextBase } from "@nextbasejs/core";
import { after } from "next/server";
import { Resend } from "resend";

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

export const nextbase = new NextBase({
mail: {
from: "app@example.com",
appUrl: "https://app.example.com",
async send({ from, to, subject, text, html }) {
await resend.emails.send({ from, to, subject, text, html });
},
},
waitUntil: after,
});
OptionDescription
fromRequired. The sender address.
sendRequired. Sends one email: { from, to, subject, text, html? }.
appUrlRequired. Your app's URL. Links point at pages there. HTTPS, except on localhost.
templatesOptional. Replaces the subject and text of any kind of email.

Without mail, the account email endpoints answer 400 in production. In development, the emails are written to the server log instead, so you can follow the links.

Links open pages in your app, at these paths under appUrl, with the token in ?token=:

EmailPathGuide
Verify your email/verify-emailEmail verification
Reset your password/reset-passwordPassword reset
Confirm your new email/confirm-email-changeEmail change

The link's address always comes from appUrl, never from the request, so an attacker cannot make NextBase send links to their own site.

Sending in the background​

Emails are sent after the response, so requests answer just as fast whether or not an account exists. On serverless platforms, background work needs waitUntil: on Next.js, pass after from next/server, as above. On Vercel, NextBase uses Vercel's own waitUntil by default.

Custom templates​

Each kind of email has a plain-text default. Replace any of them:

mail: {
// ...
templates: {
passwordReset: ({ url }) => ({
subject: "Reset your Acme password",
text: `Choose a new password: ${url}`,
html: `<p><a href="${url}">Choose a new password</a></p>`,
}),
},
},

The kinds are verification, passwordReset, emailChange, passwordChanged and emailChanged. The last two tell the owner that their password or email changed, and carry no link.