Sub topic 6
Writing the confirmation email and landing page
This chapter covers the two pieces a new subscriber sees during double opt-in: the confirmation email and the page the link opens. It assumes you have read the chapters on the subscriber lifecycle and on signed, expiring tokens. You should know how make_token and verify_token work, and that a new subscriber starts in the pending state. You should also be comfortable with HTTP GET and POST.
What these two pieces must do
The confirmation email is a transactional message. The subscriber asked for it, and it contains one action. The landing page finishes that action. Together they have four jobs:
- Make clear what the person is confirming.
- Make the action easy to complete on a phone, in a few seconds.
- Change state only when a human has deliberately acted.
- Leave a record that the confirmation happened.
Most confirmation failures come from violating one of these. A vague subject line means the person ignores the message. A link that confirms on GET means a security scanner can confirm for them. A page that shows a bare error when the token has expired loses a subscriber who wanted to join.
Writing the confirmation email
Subject line and sender
The subject line should say what to do and for what. "Confirm your subscription to Example Weekly" works. Avoid urgency tricks, emoji, and promotional language. Mailbox providers evaluate this message like any other, and a confirmation email that looks like marketing is more likely to land in spam, which means the person never sees it.
Send it from the same domain and sender name the subscriber will see on later mailings. A consistent sender builds recognition and lets the first message start building reputation for that address.
Body content
Keep the body short. It needs:
- The name of the list or publication, so the person recognises what they signed up for.
- One sentence on what they will receive and roughly how often.
- One clear button or link for confirming.
- A plain-text fallback URL, because some clients strip buttons.
- A line for people who did not sign up: "If you did not request this, ignore this email and you will not be subscribed."
- How long the link stays valid.
The line for people who did not sign up matters. Anyone can type another person's address into a form. Without confirmation, that person would receive mail they never requested. With confirmation, ignoring the message is enough to stop it. Your design relies on that outcome, so say it explicitly.
Plain text and HTML together
Send a multipart message with both a text/plain and a text/html part. The plain part keeps the message readable in strict clients and helps spam filters, which tend to distrust HTML-only mail.
from email.message import EmailMessage
BASE_URL = "https://example.com"
def build_confirmation_email(email: str, token: str) -> EmailMessage:
link = f"{BASE_URL}/confirm/{token}"
msg = EmailMessage()
msg["Subject"] = "Confirm your subscription to Example Weekly"
msg["From"] = "Example Weekly <[email protected]>"
msg["To"] = email
msg.set_content(
"Please confirm your subscription to Example Weekly.\n\n"
"You will get one email a week with new articles.\n\n"
f"Confirm here: {link}\n\n"
"This link works for 48 hours.\n"
"If you did not sign up, ignore this email and you will not be subscribed.\n"
)
msg.add_alternative(
f"""\
<p>Please confirm your subscription to <strong>Example Weekly</strong>.</p>
<p>You will get one email a week with new articles.</p>
<p><a href="{link}" style="display:inline-block;padding:12px 20px;
background:#1a56db;color:#ffffff;text-decoration:none;border-radius:4px;">
Confirm subscription</a></p>
<p>Or paste this address into your browser:<br>{link}</p>
<p>This link works for 48 hours. If you did not sign up, ignore this
email and you will not be subscribed.</p>
""",
subtype="html",
)
return msg
if __name__ == "__main__":
message = build_confirmation_email("[email protected]", "demo-token")
print(message.as_string())The token is already URL-safe because make_token uses URL-safe base64 with a dot separator, so it can go in the path without further encoding.
Things to leave out
- Tracking pixels and click-tracking redirects. Rewriting the confirmation link through a tracking domain adds a failure point and can trigger phishing filters, because the visible link and real destination differ. Link directly to your own domain.
- Marketing content. Promotions in the confirmation email blur its purpose and can undermine the claim that the message was purely transactional.
- Personal data beyond the address. The email may sit in an inbox for days. Do not include anything sensitive.
- An unsubscribe footer built for list mail. The recipient has not joined the list yet. A line saying they will not be subscribed unless they confirm is more accurate.
The landing page
GET shows, POST changes
The link in the email produces a GET request. As the chapter on link scanners explains, many corporate gateways and mail clients open every link in a message automatically. If GET confirmed the subscription, a scanner would confirm people who never saw the email.
So the GET handler only verifies the token and renders a page with a button. The button submits a form by POST, and only the POST handler changes the database. The handlers from the earlier chapter already do this:
def show_confirmation_page(token: str):
email = verify_token(token, "confirm")
if email is None:
return render("link_invalid_or_expired.html")
return render("confirm_button.html", token=token)The matching form in confirm_button.html is small:
<h1>Confirm your subscription</h1>
<p>You are about to subscribe to Example Weekly: one email a week.</p>
<form method="post" action="/confirm/{{ token }}">
<button type="submit">Yes, subscribe me</button>
</form>Showing the list name and frequency again repeats what the person is agreeing to. That text is part of the consent, so store it in the consent_text column of the confirm event.
Idempotent confirmation
People double-click, reload, and open the email on two devices. The UPDATE ... WHERE status = 'pending' in the POST handler makes repeats harmless. The second request matches no rows, and the person still sees the success page. Record the confirm event only when a row was actually updated, so repeat clicks do not fill the audit trail with duplicates.
States the page must handle
| Situation | What `verify_token` returns | What to show |
|---|---|---|
| Valid token, status `pending` | The email | Button, then the success page |
| Valid token, already `confirmed` | The email | Success page, with no error |
| Expired token | `None` | Explain the link expired and offer a new one |
| Tampered or malformed token | `None` | The same generic invalid-link page |
| Valid token, status `unsubscribed` | The email | Treat as a fresh request (see the warning below) |
The expired and tampered cases share one response on purpose. Telling a visitor which one it was gives an attacker information about your token format.
The expired-link page
An expired link is a common, recoverable situation. A page that says only "Invalid link" loses a person who wanted to join. Offer a form asking for the email address, and send a new confirmation email if that address has a pending record. Show the same message whether or not the address exists, such as "If this address is waiting for confirmation, we have sent a new link." That prevents anyone from using the form to learn who is on your list. The chapter on resends covers rate limits and expiry rules in detail.
The success page
After the POST, show a page that:
- States plainly that the subscription is confirmed.
- Says what happens next, such as when the first issue arrives.
- Links to the preference centre, so the person can set topics and frequency right away.
Offering preferences at this point works well because attention is highest just after confirming. It also lowers later unsubscribes, since people can choose less before they get annoyed.
Security details for the page
- Set
Referrer-Policy: no-referreron the confirm page, so the token in the URL is not sent to third-party resources the page loads. - Avoid third-party scripts and trackers on this page for the same reason.
- Do not log full URLs containing tokens. Log a hash or the first few characters.
- Serve everything over HTTPS only.
- Consider a CSRF token on the form. The signed confirmation token already limits what a forged POST can do, but a CSRF token costs little.
Testing the flow
Before shipping, check these cases:
- Open the link with
curl(a GET) and confirm the database is unchanged. - Submit the POST twice and confirm there is one
confirmevent. - Change one character of the token and confirm the invalid-link page appears.
- Wait past expiry, or set
ttl_secondsto1in a test, and confirm the expired page offers a resend. - View the email in a plain-text client and with images blocked. The link must still be usable.
Exercise
A customer's email gateway opens every link in incoming mail to scan it. Your confirmation link currently runs UPDATE subscribers SET status = 'confirmed' when it receives the GET request. What goes wrong, and what is the fix?
Show answer
The gateway's automatic GET confirms the subscription before the person has seen the email, so the list contains an address that never gave real consent, and the audit trail shows a confirmation the person did not make. The fix is to make the GET handler only verify the token and show a page with a button, and to move the UPDATE into a POST handler that runs when the person clicks the button.
Exercise
Why should the expired-link page show the same message whether or not the submitted email address is on your list?
Show answer
A different message for known and unknown addresses would let anyone test whether a given person is on your list, which leaks information. A neutral message, such as "If this address is waiting for confirmation, we have sent a new link", reveals nothing.