Sub topic 18
Privacy, consent and compliance
This chapter assumes you know the routing and preference checks from the earlier chapters and can read short Python snippets. It covers how the system proves a user agreed to a message and how it handles personal data. It is engineering guidance, not legal advice. Confirm the rules for your markets with a lawyer.
Consent is more than a preference
A preference says what a user would like to receive. Consent is a record that the user agreed, which you may need to show to a regulator. Rules such as GDPR in the EU, CAN-SPAM for US email and TCPA for US SMS differ in detail. They share a few demands: get permission for marketing, make opting out easy, and honor opt-outs promptly.
Store consent as an append-only log, not a single boolean. Each entry holds:
- the user and the channel
- the category, such as marketing or transactional
- granted or revoked
- a timestamp and the source, such as a signup form or an SMS reply
- the version of the consent text the user saw
The latest entry for a user, channel and category decides the current state.
Check consent at send time
Check consent in the worker just before the provider call, not only at ingestion. A user may opt out while an event waits in the queue.
from dataclasses import dataclass
from datetime import datetime, timezone
@dataclass(frozen=True)
class ConsentEntry:
user_id: str
channel: str
category: str
granted: bool
at: datetime
source: str
def has_consent(entries, user_id, channel, category):
"""Return True if the latest matching entry grants consent."""
matching = [
e for e in entries
if (e.user_id, e.channel, e.category) == (user_id, channel, category)
]
if not matching:
return False
return max(matching, key=lambda e: e.at).granted
log = [
ConsentEntry("user-42", "sms", "marketing", True,
datetime(2026, 1, 5, tzinfo=timezone.utc), "signup_form"),
ConsentEntry("user-42", "sms", "marketing", False,
datetime(2026, 3, 9, tzinfo=timezone.utc), "sms_stop"),
]
print(has_consent(log, "user-42", "sms", "marketing")) # FalseMissing consent means no send. Transactional messages, such as a password reset, are often allowed without marketing consent. Keep the two categories separate, so a marketing opt-out never blocks a security alert.
Handle opt-outs and suppression
Opt-outs arrive through unsubscribe links, SMS replies such as STOP, and provider feedback such as spam complaints or hard bounces. Write each of them to the consent log and to a suppression list of addresses and numbers you must not contact. Process them quickly, because legal deadlines for honoring an opt-out can be short.
Minimize and protect personal data
- Keep sensitive content out of push and SMS text. Lock screens and carrier networks are not private.
- Log event IDs and statuses, not message bodies, emails or phone numbers.
- Encrypt contact details and device tokens at rest, and limit which services can read them.
- Set retention periods for delivery logs and delete data when they expire.
When a user asks for deletion, remove their profile and tokens. Keep a minimal suppression entry, ideally a hash of the address, so you do not message them again.
Exercise
A user opts out of marketing email while a promotional email sits in the queue. Where should the system catch this, and why?
Show answer
In the worker, right before the provider call. A check made only at ingestion would miss the opt-out, because it happened after the event was accepted. The worker should read the latest consent entry and drop the message with a logged reason.