Sub topic 6
Writing the confirmation email and landing page
This chapter assumes you can already create signed, expiring tokens (the previous chapter) and know the difference between HTTP GET and POST. Here you write the message that carries the token and the page that receives it.
What the confirmation email must do
The confirmation email has one job: get a person who just signed up to confirm that the address is theirs and that they want the list. Everything in it should serve that job.
- Subject: name the action and the list, such as "Confirm your subscription to Example Weekly". Avoid urgency tricks, which look like spam.
- Sender: use the same From name and address as your regular mail. Subscribers then recognise it, and the message builds reputation for the right sending identity.
- Body: say what they signed up for, roughly how often you will write, and one clear button or link.
- Fallback: repeat the full URL as plain text, and send a plain-text part alongside the HTML part.
- Ignore instruction: say that they can ignore the message if they did not sign up. Someone may have typed their address by mistake, and nothing happens unless they act.
- No marketing content: do not add promotions to this message. It exists only to confirm the request.
from email.message import EmailMessage
def build_confirmation_email(email: str, token: str, base_url: 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(
"You asked to receive Example Weekly, about one email a week.\n\n"
f"Confirm your subscription: {link}\n\n"
"If you did not sign up, ignore this message. "
"You will not be subscribed.\n"
)
msg.add_alternative(
"<p>You asked to receive Example Weekly, about one email a week.</p>"
f'<p><a href="{link}">Confirm my subscription</a></p>'
f"<p>Or paste this address into your browser: {link}</p>"
"<p>If you did not sign up, ignore this message. "
"You will not be subscribed.</p>",
subtype="html",
)
return msg
if __name__ == "__main__":
message = build_confirmation_email(
"[email protected]", "TOKEN", "https://example.com"
)
print(message["Subject"])The landing page
The link in the email opens a landing page. This page must not subscribe anyone on a plain GET request, because security scanners and prefetching mail clients open links automatically. The next chapter covers that problem in depth. For now, follow this pattern:
- GET
/confirm/<token>verifies the token and shows a page with a Confirm button. It changes nothing. - The button submits a POST to the same address. The POST confirms the subscriber and records a consent event.
- The page then shows a result.
What each page state should say
| Situation | What to show |
|---|---|
| Valid token, still pending | Confirm button and the list name |
| Already confirmed | "You're already subscribed", with no error |
| Expired or invalid token | A plain explanation and a way to request a new link |
| Successful confirmation | What happens next, and when the first email arrives |
An expired link should never leave the person at a dead end. Resends are covered in a later chapter.
Exercise
Why does the confirmation link open a page with a button instead of confirming the subscription directly on GET?
Show answer
Security scanners and some mail clients open links automatically with GET requests. If GET confirmed the subscription, a scanner could confirm someone who never agreed. Making the POST from the button the only state change means a person had to act.