Skip to content
← All posts

·

How to send verification and password reset emails

Send sign-up and password reset emails from your app: provider, SPF and DKIM DNS records, templates and dev mode. What Launchfast already does.

Short answer: pick an email provider, verify your sending domain with the DNS records it gives you (SPF and DKIM), write one function that sends a message, and call it from your auth library's "send verification" and "send reset password" hooks. Launchfast already does this with Resend: sendEmail() in src/lib/email.ts, two ready templates through actionEmail(), emails printed to the terminal in local dev, and npm run setup adds the domain and its DNS records for you.

Why do verification and reset emails break?

The code is short. The failures come from everything around it:

  • The email lands in spam, or never arrives, because the sending domain has no SPF or DKIM records.
  • Resend rejects the send with a "domain is not verified" error, because from uses a domain you haven't verified yet.
  • It works on your laptop and fails in production because the API key was never added to the host.
  • You can't test sign-up locally without a real mailbox, so you skip testing it.
  • The reset link is built with the wrong base URL and points at localhost.

How do you do it by hand?

  1. Choose a provider. Resend, Postmark, SES and others all work. You need an API key and an official SDK or HTTP API.

  2. Add your domain in the provider's dashboard. Use a domain you own. A shared test sender is fine for a first try, but it can only send to limited addresses, so real users need your own domain.

  3. Publish the DNS records. The provider lists them: DKIM records (a TXT record with a public key), and usually SPF and a return-path record on a subdomain. Names are relative to your domain in some dashboards and absolute in others, so a record pasted into the wrong field quietly does nothing.

  4. Wait for verification and click verify. DNS can take a few minutes or longer. Until the domain shows as verified, sends from it fail.

  5. Write a send function. Wrap the SDK so the rest of the app calls one function:

    import { Resend } from "resend";
    const resend = new Resend(process.env.RESEND_API_KEY);
    await resend.emails.send({ from, to, subject, html, text });
    

    Send both html and plain text. Check the returned error; the SDK returns it instead of throwing.

  6. Write the templates. One for confirming an email address and one for resetting a password. Each needs a button, a plain link as a fallback, and a line saying it can be ignored if the person didn't ask. Escape any user-supplied value that goes into the HTML.

  7. Hook it into auth. Your auth library needs two callbacks: one that receives the user and a verification URL, one that receives the user and a reset URL. Send the link in each.

  8. Set the app's base URL. The links are built from it, so it must be your real domain in production.

  9. Add a dev fallback. Without one, every developer needs an API key just to sign up locally.

  10. Handle the "already registered" case. If someone signs up with an email that has an account, a different response reveals which emails are registered. Return the same response and send the owner an email instead.

  11. Copy the key to production. Add RESEND_API_KEY and the sender address to your host's env vars.

The traps are steps 3, 4, 9 and 10.

What does Launchfast already do?

Launchfast sends through Resend. The pieces are in the repository.

One send function. sendEmail() in src/lib/email.ts wraps the Resend SDK and throws a clear error if Resend returns one:

export async function sendEmail(email: Email) {
  const apiKey = process.env.RESEND_API_KEY;
  if (!apiKey) {
    if (isProduction) throw new Error("Missing environment variable RESEND_API_KEY.");
    console.info(`\n[email] to=${email.to} subject="${email.subject}"\n${email.text}\n`);
    return;
  }
  resend ??= new Resend(apiKey);
  const from = process.env.EMAIL_FROM ?? `${siteConfig.name} <onboarding@resend.dev>`;
  const { error } = await resend.emails.send({ from, ...email });
  if (error) throw new Error(`Email to ${email.to} failed: ${error.message}`);
}

Dev mode needs no key. With no RESEND_API_KEY, the email's text version is printed to the terminal where npm run dev runs, so you can sign up and click the link from the log. In production the same missing key throws instead of silently dropping the email.

A template for both emails. actionEmail() builds a single-button email with an HTML and a plain-text version, a fallback link, and an "if you didn't request this" line. It HTML-escapes the values it inserts.

Wired into Better Auth. In src/lib/auth.ts, sendVerificationEmail and sendResetPassword call sendEmail(actionEmail({ … })):

sendResetPassword: async ({ user, url }) => {
  await sendEmail(
    actionEmail({
      to: user.email,
      subject: `Reset your ${siteConfig.name} password`,
      intro: "Someone asked to reset the password for your account.",
      cta: "Reset password",
      url,
    }),
  );
},

Sign-up requires a confirmed email (requireEmailVerification: true), a confirmation email goes out on sign-up and again on sign-in if the address is still unconfirmed, and a password reset signs out the account's other sessions (revokeSessionsOnPasswordReset: true). The forgot-password and reset-password pages are in src/app/(auth)/.

The already-registered case is covered. onExistingUserSignUp sends a fresh confirmation link if the address was never confirmed, and otherwise emails the owner a "You already have an account" message with a sign-in link. The sign-up response stays the same either way.

npm run setup does the DNS work. The "Email" step asks for your Resend API key and sending domain, then scripts/setup/email.mts adds the domain to Resend (or finds it if it's already there) and fetches the records to publish. If your DNS is on Cloudflare, it creates them with a Cloudflare API token and skips records that already exist. Otherwise it prints the records for you to add at your DNS provider. It then asks Resend to verify the domain and saves RESEND_API_KEY and EMAIL_FROM to .env.local. On deploy, it copies them to Vercel.

What should you know before relying on this?

DNS verification still takes however long your DNS provider and Resend take, so the first live email may have to wait a few minutes. The setup step starts verification, and you can check the status at resend.com/domains. Without EMAIL_FROM, the kit sends from Resend's onboarding@resend.dev address, which is meant for testing, so set a sender on your own domain before launch.

How much does this save?

You skip the send wrapper, the templates, the auth hooks, the dev fallback and the manual DNS copying. What is left is a Resend API key and a domain you own.

Launchfast is the starter this post describes. For the other pieces of a launch, read how to add Google and GitHub login to your web app, how to set up a Postgres database for your web app and the checklist I run before launching a SaaS.