Use-cases

Password reset emails: a template, working code and security rules

Short answer

A password reset email sends a single-use, short-lived link to the account's email address. Generate a random token, store only its hash, expire it quickly, and show the same message whether or not the address exists. Send it from a verified domain through a transactional email API so it arrives in seconds, with a plain subject, one clear button and a text version.

Last checked 16 November 2026

A password reset email gives someone who forgot their password a single-use link to choose a new one. The link carries a random token that expires quickly and works once. Security lives in how you create and check that token; deliverability lives in how you send the email. Get both right and the reset arrives in seconds and cannot be reused.

How a secure reset flow works

  1. The user enters an email address on your “Forgot password” form.
  2. Your app shows the same message whatever happens: “If an account exists for that address, we have emailed a link.”
  3. If the account exists, generate a random token (at least 128 bits, from a secure random source), store a hash of it with an expiry time, and email the link.
  4. When the link is opened, hash the token from the URL, look it up, check it has not expired or been used, and show the new password form.
  5. On success, mark the token used, invalidate other reset tokens, and sign out other sessions.

The OWASP Forgot Password Cheat Sheet covers each step in more depth.

Best practices for the email itself

  • Plain subject. “Reset your Acme password” beats anything clever. People search for it.
  • One button, plus the raw link. Some clients block buttons; the plain URL is the fallback.
  • Say when it expires. “This link works for 30 minutes.”
  • Say what to do if it was not them. “You can ignore this email. Your password has not changed.”
  • No tracking on the link. Click tracking rewrites URLs through another domain, which looks suspicious in a security email and can break the token. Send the link as it is.
  • Send it immediately. Never batch or delay resets. Use a queue only for retries.
  • Separate it from marketing. Use a transactional stream or subdomain so newsletter complaints never slow down resets.

Password reset email template

From: Acme <security@acme.com>
Subject: Reset your Acme password

Hi Sam,

Someone asked to reset the password for your Acme account (sam@example.com).

Reset your password:
https://app.acme.com/reset?token=Qm9n...7Xa

This link works once, for 30 minutes.

If you did not ask for this, ignore this email. Your password has not changed.

Acme, 1 Example Street, Springfield

Sending the reset email with Node.js

This example uses the Mailcandor email API. The token logic is plain Node.js; the send is one fetch call.

import crypto from 'node:crypto';

export async function sendPasswordReset(user, db) {
  const token = crypto.randomBytes(32).toString('base64url');
  const hash = crypto.createHash('sha256').update(token).digest('hex');
  await db.resetTokens.insert({
    userId: user.id,
    hash,
    expiresAt: new Date(Date.now() + 30 * 60 * 1000),
  });

  const link = `https://app.acme.com/reset?token=${token}`;
  const res = await fetch('https://api.mailcandor.com/v1/emails', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.MAILCANDOR_KEY}`,
      'Content-Type': 'application/json',
      'Idempotency-Key': `reset-${hash.slice(0, 32)}`,
    },
    body: JSON.stringify({
      from: 'Acme <security@acme.com>',
      to: user.email,
      subject: 'Reset your Acme password',
      html: `<p>Hi ${user.firstName},</p>
<p>Someone asked to reset the password for your Acme account.</p>
<p><a href="${link}">Reset your password</a></p>
<p>Or paste this link: ${link}</p>
<p>This link works once, for 30 minutes. If you did not ask for this, ignore this email.</p>`,
      text: `Reset your password: ${link}\n\nThis link works once, for 30 minutes. If you did not ask for this, ignore this email.`,
      tags: [{ name: 'flow', value: 'password_reset' }],
    }),
  });
  if (!res.ok) {
    const { error } = await res.json();
    throw new Error(`${error.type}: ${error.message}`);
  }
}

The Idempotency-Key makes a retry after a timeout return the first response instead of sending a second email. The flow tag lets you filter delivery webhooks to resets only, so you can alert if they start bouncing.

Django: send password reset email over SMTP

Django’s built-in PasswordResetView already generates tokens and sends the email. Point it at an SMTP relay in settings.py:

import os

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.mailcandor.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = "mailcandor"
EMAIL_HOST_PASSWORD = os.environ["MAILCANDOR_KEY"]
DEFAULT_FROM_EMAIL = "Acme <security@acme.com>"
PASSWORD_RESET_TIMEOUT = 1800  # seconds

Override registration/password_reset_email.html and password_reset_subject.txt to use the wording above.

Send a confirmation after the change

Once the new password is saved, send a second, short email: “Your Acme password was changed on 16 November at 14:02 UTC. If this was not you, contact security@acme.com.” It costs nothing and is often the first sign of a taken-over account. Do not include a link that undoes the change without signing in; point people to support instead.

Where Mailcandor fits

With Mailcandor you can give the code that sends resets a sending-only API key locked to one domain, so a leak cannot send as anything else. Delivery, bounce and failure events come back by webhook with the email id, and the deliverability coach explains bounces in plain English. Mailcandor is in limited access; see pricing to request it.

Common pitfalls

  • Storing the raw token. If your database leaks, every pending reset is usable. Store the hash.
  • Long-lived links. A link valid for days sits in an inbox waiting to be found.
  • Different messages for unknown emails. This leaks which addresses have accounts.
  • Host header injection. Build the link from a configured base URL, not the request’s Host header, or an attacker can make your email point to their site.
  • No rate limit. Without one, your form can be used to flood someone’s inbox. Limit per address and per IP.
  • Unverified sending domain. The single biggest cause of resets landing in spam. See why emails go to spam.

Questions

How long should a password reset link be valid?

Short. Many apps use 15 to 60 minutes. The link should also stop working once it is used or once a newer reset is requested.

Should the reset email say whether the account exists?

No. Show the same on-screen message for every address, such as: if an account exists, we have sent a link. Otherwise attackers can use the form to discover which emails have accounts.

Why do password reset emails go to spam?

Usually because the sending domain lacks SPF, DKIM or DMARC, the email shares a domain with marketing mail that gets complaints, or the link points to a URL shortener or a domain unrelated to the sender.

How do I send a password reset email in Django?

Django's built-in PasswordResetView sends the email through whatever EMAIL_BACKEND you configure. Point the SMTP settings at a relay and verify your domain.

Should I put the user's password in the email?

Never. Send a link to set a new password. Email is not a safe place for any secret that lasts.

Related

Send with someone on your side.

Unlimited domains on every plan, a person before any block, and 3,000 emails a month free. Tell us what you send and we set you up within one business day.