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
- The user enters an email address on your “Forgot password” form.
- Your app shows the same message whatever happens: “If an account exists for that address, we have emailed a link.”
- 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.
- 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.
- 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
Hostheader, 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.