Sub topic 6
Confirming consent with double opt-in
This chapter assumes you can read a simple HTML form and a short Python snippet. It also assumes you know what a sign-up form collects and why consent matters. It is general education, not legal advice.
Single versus double opt-in
With single opt-in, the user submits the form and is added to your list right away. You never learn whether the address belongs to the person who typed it.
With double opt-in, the sign-up only creates a pending record. You then email a confirmation link to that address. The address becomes active only after someone clicks the link.
That second step proves the owner of the mailbox agreed. It also catches typos and addresses entered by someone else.
| Row | Single opt-in | Double opt-in |
|---|---|---|
| Steps for the user | Submit the form | Submit the form, then click an emailed link |
| Proof the address owner agreed | Weak | Strong |
| Typos and fake addresses | Enter the list | Mostly filtered out |
| Sign-ups lost | Fewer | Some users never confirm |
Double opt-in is not named as a requirement in GDPR, CAN-SPAM, or CASL. Regulators in some EU countries expect it or strongly favor it, and it is often the easiest way to show that consent came from the right person. Check the rules for the regions you send to.
The flow
Build the signed link
The confirmation link must not be guessable. If it were only /[email protected], anyone could confirm any address. Instead, include a token that only your server can produce. The token is a signature over the email address and the time it was issued.
import hashlib
import hmac
SECRET_KEY = b"load-this-from-an-environment-variable"
MAX_AGE_SECONDS = 48 * 60 * 60
def make_token(email: str, issued_at: int) -> str:
message = f"{email}|{issued_at}".encode()
signature = hmac.new(SECRET_KEY, message, hashlib.sha256).hexdigest()
return f"{issued_at}.{signature}"
def verify_token(email: str, token: str, now: int) -> bool:
try:
issued_text, signature = token.split(".", 1)
issued_at = int(issued_text)
except ValueError:
return False
if now - issued_at > MAX_AGE_SECONDS:
return False
message = f"{email}|{issued_at}".encode()
expected = hmac.new(SECRET_KEY, message, hashlib.sha256).hexdigest()
return hmac.compare_digest(signature, expected)How it works:
make_tokensigns the email and timestamp with HMAC-SHA256, using a secret key only your server knows.verify_tokenrebuilds the signature from the email in the request and compares it with the one in the link. A changed email or timestamp produces a different signature, so the check fails.- The age check rejects links older than 48 hours, so an old email sitting in a mailbox cannot be used later.
hmac.compare_digestcompares the values in constant time, which avoids leaking information through response timing.
What to do when the user clicks
- Read the email and token from the link's query string.
- Call
verify_tokenwith the current time. - If it passes, set the record's
confirmed_attimestamp and change its status from pending to confirmed. - If it fails or has expired, show a clear message and offer to send a new link.
- Handle repeat clicks gracefully. A second click on a valid link should say the address is already confirmed, not show an error.
Do not send any marketing email to a pending address. The only message it should receive is the confirmation email itself. Keep that email plain: say what the person signed up for, include the link, and add no promotions.
Common mistakes
- Treating a pending address as subscribed. Mail sent before confirmation defeats the purpose of the check.
- Links that never expire. Old links can be clicked by someone with access to the mailbox long after sign-up.
- Pre-confirming on page load. Some security scanners open every link in an email automatically. If a plain GET request confirms the address, a scanner can confirm it for the user. A safer design makes the link open a page with a "Confirm my subscription" button that sends a POST request.
- Not storing the confirmation time. The
confirmed_atvalue is part of your proof of consent, covered in the next chapters. - Endless reminders. Send at most one reminder. Delete pending records that are never confirmed after the link expires.
Exercise
A user signs up on Monday. Your server emails a confirmation link, and the user clicks it on Friday, five days later. With the 48-hour limit in verify_token, what happens, and what should the user see?
Show answer
verify_token returns False because now - issued_at is greater than 48 hours. The address stays pending and no marketing email is sent. The user should see a message that the link has expired, with an option to request a new confirmation email.
Exercise
Your confirmation link confirms the address as soon as the page loads (a GET request). Why can this cause problems, and what change fixes it?
Show answer
Email security scanners often open links automatically, so a scanner could confirm an address the owner never approved. Make the link open a page with a confirm button that submits a POST request, so only a deliberate user action completes the confirmation.