Sub topic 2
Writing the Confirmation Email and Landing Page
Assume you understand HTTP GET and POST requests, basic SQL, and how your email provider sends messages. The confirmation flow should prove that the person who entered an address can access that mailbox. It should also make accidental state changes difficult.
Write a clear confirmation email
The email should state:
- Why the person received it.
- What they will receive after confirming.
- A single, prominent Confirm subscription action.
- The address being confirmed.
- How long the link remains valid.
- How to contact you if the signup was unexpected.
The link should open a landing page. A GET request should only display the page. The page should contain a form or button that sends a POST request. The POST handler verifies the signed, purpose-specific token and changes pending to confirmed.
def confirm_subscription(db, token):
email = verify_token(token, purpose="confirm")
if email is None:
return "This confirmation link is invalid or expired.", 400
db.execute(
"""UPDATE subscribers
SET status = 'confirmed', confirmed_at = CURRENT_TIMESTAMP
WHERE email = %s AND status = 'pending'""",
(email,),
)
db.execute(
"""INSERT INTO consent_events
(subscriber_id, event_type, source, occurred_at)
SELECT id, 'confirm', 'confirmation_page', CURRENT_TIMESTAMP
FROM subscribers WHERE email = %s""",
(email,),
)
return "Your subscription is confirmed.", 200Survive scanners and link prefetching
Mail security tools often open links automatically. If a GET request confirms a subscription or unsubscribes someone, the tool can change a subscriber's state without their choice. Keep GET requests read-only and require a deliberate POST. Make confirmation idempotent, meaning repeated requests produce the same result.
> callout warning > Never place an irreversible subscription change in an email link's GET request.
Handle resends and expiry
Let a subscriber request another message without creating duplicate subscriber records. Replace or invalidate the previous token, apply a short expiry such as 48 hours, and rate-limit resends per address and IP address. An expired signup can remain pending; clean it up later or let a new signup replace it.
Keep an audit trail
Record the event type, timestamp, source, consent wording or policy version, and subscriber identifier. Store IP address and user agent only when appropriate for your privacy obligations. Do not silently overwrite earlier consent events.
Make unsubscribing reliable
Every marketing message should offer a visible unsubscribe link. The unsubscribe endpoint should work without login, verify a purpose-specific token, record an unsubscribe event, and immediately add the address to a suppression list. Apply that list before every send. Support a preference centre for topic or frequency changes, but never require it to complete an unsubscribe.
Exercise
Why should a confirmation email's link open a page before changing subscriber state?
Show answer
Security scanners may fetch links automatically, so only a deliberate POST should change state.