Sub topic 4
Modelling the subscriber lifecycle
This chapter assumes you know basic SQL, can read Python, understand HTTP GET and POST, and have a general idea of what spam complaints are. It covers the states a subscriber moves through and how to store them. Token signing, the confirmation email, and unsubscribe handling come in later chapters.
Why model states explicitly
A list is not a column of email addresses. Each address has a history. Someone has asked to subscribe but not yet confirmed, or has confirmed, or has left. If your code only knows "on the list" or "not on the list", it will eventually mail someone who never confirmed, or re-add someone who unsubscribed.
A small set of named states makes those rules checkable. Every send, import, and form submission asks the same question: what state is this person in, and is this change allowed?
The three states
| State | Meaning | May receive marketing email? |
|---|---|---|
| pending | Signed up, has not confirmed yet | No. Only the confirmation message. |
| confirmed | Clicked the confirmation link and pressed the button | Yes |
| unsubscribed | Asked to stop | No |
In single opt-in, new subscribers can start as confirmed. In double opt-in, they always start as pending.
Allowed transitions
Two details matter here. First, an unsubscribed person never goes straight back to confirmed. A new sign-up sends them to pending, so they confirm again and you get fresh evidence of consent. Second, a repeated confirmation changes nothing. Email clients and people both click links twice.
The schema
The status column holds the current state. A CHECK constraint rejects any value outside the three allowed ones.
CREATE TABLE subscribers (
id BIGSERIAL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
status TEXT NOT NULL DEFAULT 'pending'
CHECK (status IN ('pending', 'confirmed', 'unsubscribed')),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
confirmed_at TIMESTAMPTZ
);The default is pending, so a code path that forgets to set a status fails safe: the person gets no marketing mail. The UNIQUE constraint on email means a second sign-up with the same address updates the existing row instead of creating a duplicate.
The row stores only the current state. It does not say when, where, or why a change happened. That belongs in a separate append-only table of consent events, which a later chapter builds.
Enforcing transitions in the query
Put the allowed "from" state in the WHERE clause. The update then does nothing if the person is in the wrong state:
UPDATE subscribers
SET status = 'confirmed', confirmed_at = now()
WHERE email = '[email protected]'
AND status = 'pending';An unsubscribed address matches no row, so it stays unsubscribed. Running the statement twice is also safe, because the second run finds no pending row. Check the affected row count if you need to tell "confirmed just now" apart from "nothing to do".
Common errors
- Starting everyone as confirmed. This skips double opt-in. Make
pendingthe default. - Deleting unsubscribed rows. You lose the record that stops you from mailing them again. Keep the row with status
unsubscribed. - Re-activating on import. Importing an old list must not move
unsubscribedrows back toconfirmed. - Mailing
pendingaddresses. Select recipients withstatus = 'confirmed', never withstatus != 'unsubscribed'.
Exercise
A person unsubscribed last year. Today they submit your sign-up form again with the same address. What should their status become, and why not confirmed?
Show answer
Their status should become pending, and a confirmation email is sent. Moving straight to confirmed would skip the proof that the person who typed the address controls the inbox. Going through pending gives you fresh, verifiable consent.