Sub topic 8
Handling resends, expiry, and abandoned sign-ups
This chapter assumes you have the subscribers table and the signed token functions from the earlier chapters. You should also be comfortable with HTTP forms and basic SQL. Here you decide what happens when a confirmation email goes unanswered.
Why pending sign-ups need a plan
A subscriber in the pending state has not given consent yet. Confirmation tokens expire (48 hours in our make_token default), so an old pending row is a dead end unless you handle it. Three things can go wrong:
- The email lands in spam or is never opened, so the person never confirms.
- The link expires before the person clicks it.
- The person clicks the same link twice, or signs up again.
The lifecycle of an unconfirmed sign-up
Resending safely
A resend should issue a new token. It should not extend the old one. Because tokens are stateless and signed, the old token stays valid until its own expiry, which is harmless: confirming twice does nothing, as the WHERE status = 'pending' clause in confirm_subscription already guarantees.
Limit resends, or your form becomes a way to mail-bomb someone else's inbox. Track the last send time and a counter:
ALTER TABLE subscribers
ADD COLUMN confirmation_sent_at TIMESTAMPTZ,
ADD COLUMN confirmation_send_count INT NOT NULL DEFAULT 0;from datetime import datetime, timedelta, timezone
MAX_SENDS = 3
MIN_GAP = timedelta(minutes=10)
def resend_confirmation(db, email: str, send_email) -> None:
"""Send a new confirmation email if the rules allow it.
The caller always shows the same neutral message, so the form
does not reveal whether an address is already on the list.
"""
row = db.query_one(
"SELECT status, confirmation_sent_at, confirmation_send_count "
"FROM subscribers WHERE email = %s",
(email,),
)
if row is None or row["status"] != "pending":
return
now = datetime.now(timezone.utc)
last = row["confirmation_sent_at"]
if row["confirmation_send_count"] >= MAX_SENDS:
return
if last is not None and now - last < MIN_GAP:
return
send_email(email, make_token(email, "confirm"))
db.execute(
"UPDATE subscribers SET confirmation_sent_at = %s, "
"confirmation_send_count = confirmation_send_count + 1 "
"WHERE email = %s",
(now, email),
)The function returns silently in every refused case. Showing the same message ("If this address needs confirmation, we have sent an email") for all outcomes avoids leaking who is subscribed.
Expired links
When verify_token returns None, the landing page cannot tell whether the token was forged or just old. Show one page for both cases. It should explain that the link may have expired and offer a form to request a new email. Do not pre-fill or trust any address taken from the failed token.
Abandoned sign-ups
Pending rows that never confirm are not subscribers, and you should not mail them marketing. Run a scheduled clean-up that removes them after a retention window:
DELETE FROM subscribers
WHERE status = 'pending'
AND created_at < now() - INTERVAL '30 days'
AND NOT EXISTS (
SELECT 1 FROM consent_events e
WHERE e.subscriber_id = subscribers.id
AND e.event_type <> 'signup'
);Deleting a row that has consent_events entries would break the foreign key. Decide your policy first. One option is to delete the signup events along with the row, since no consent was ever given. Another is to keep a minimal record in a separate log if you need to show that you honoured a deletion or a prior complaint. Whichever you choose, apply it consistently and document it.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| No resend limit | Form can be used to flood a victim's inbox | Cap sends and enforce a minimum gap |
| Resend reveals "already subscribed" | Anyone can test who is on your list | Return one neutral message |
| Extending the expiry of old tokens | Old links in forwarded emails stay usable | Issue a new token instead |
| Keeping pending rows forever | Stale personal data and a cluttered funnel | Purge after a fixed window |
Exercise
A visitor submits the sign-up form four times in five minutes with the same address. With MAXSENDS = 3 and MINGAP = 10 minutes, how many confirmation emails are sent in total, and does the subscriber become confirmed?
Show answer
One email is sent. The first sign-up sends it and sets confirmationsentat. Each later attempt falls inside the 10-minute gap, so resend_confirmation returns without sending. The subscriber stays pending, because submitting a form never confirms anyone. Only clicking the link in the email and posting the confirmation does.