Skip to content

Honour opt-outs during sends, one-click unsubscribe, escape merge tags - #17

Merged
op-techy merged 1 commit into
mainfrom
fix/send-compliance
Dec 4, 2026
Merged

op-techy merged 1 commit into
mainfrom
fix/send-compliance

Conversation

@op-techy

@op-techy op-techy commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Fixes four send-compliance problems found in an audit.

Problems

  1. Opt-outs during a send were ignored. Eligibility was checked only when the queue was built. Anyone who unsubscribed, bounced or complained during a long send still got the email.
  2. No one-click unsubscribe. Emails had no List-Unsubscribe or List-Unsubscribe-Post headers, which Gmail and Yahoo require from bulk senders. Also, GET /track/unsubscribe/{token} unsubscribed straight away, so link scanners (Outlook Safe Links, Mimecast) unsubscribed people without their knowledge.
  3. Opt-outs weren't recorded consistently. Unsubscribing through the email link didn't add the address to the suppression list, and RecipientService didn't normalise addresses. Together, these let a mixed-case re-add mail someone who had opted out.
  4. Merge tags weren't escaped. Imported names and companies went into the email HTML as-is, so they could inject markup or links.

Changes

  • Worker: checks recipient status and the suppression list again before each send. Emails for anyone who opted out are marked with a new skipped queue status and counted in campaign_jobs.skipped_count. Both come from the new migration V2__queue_skipped.sql. job-status reports the skipped count, and progress includes skipped emails.
  • Headers: EmailSender gains a send(..., headers) overload. Resend and SMTP set the headers directly. SES switches from SendEmail to SendRawEmail, which is the only way to set custom headers, and still returns the SES message id for webhook matching.
  • Unsubscribe: GET /track/unsubscribe/{token} now shows a confirmation page and changes nothing. POST to the same URL does the unsubscribe. It handles both the page's button and RFC 8058 one-click.
  • New SuppressionService: every opt-out goes through it (email link, API, permanent bounce, complaint). It sets the recipient's status only if they are active, and adds the address to the suppression list with INSERT … ON CONFLICT DO NOTHING.
  • RecipientService: normalises addresses, and refuses to re-add recipients who complained.
  • Merge tags: values are HTML-escaped.
  • README: updated to match.

Deploy notes

  • V2__queue_skipped.sql runs at startup. It replaces the status check on email_queue and adds a column with a default.
  • The SES IAM user needs ses:SendRawEmail. Without it, SES sends fail and fall back to SMTP.

Testing

  • ./gradlew test, integrationTest and scaleTest all pass locally.
  • New unit tests:
    • the worker skips opted-out recipients and sends the one-click headers;
    • the headers reach the message through both SES (parsed raw MIME) and SMTP;
    • SuppressionService normalises addresses and changes only active recipients;
    • recipient adds are rejected in a different capitalisation, or after a complaint.
  • New integration tests on Postgres:
    • a one-click unsubscribe during a send skips that email, the campaign still finishes with 100% progress, and the address is suppressed;
    • a GET on the unsubscribe link changes nothing;
    • unsubscribing twice leaves one suppression row;
    • recipient data can't add markup to the email.

…merge tags

- The send worker checks each email again before sending it: if the
  recipient unsubscribed, bounced or complained after the campaign was
  queued, the email is marked 'skipped' (new status, V2 migration) and
  counted in campaign_jobs.skipped_count instead of being sent.
- Campaign emails carry List-Unsubscribe and List-Unsubscribe-Post
  (RFC 8058 one-click, required by Gmail and Yahoo for bulk senders).
  EmailSender takes extra headers; SES moves to SendRawEmail for them.
- GET /track/unsubscribe/{token} now only shows a confirmation page, so
  link scanners can't unsubscribe people; POST does the unsubscribe.
- New SuppressionService: every opt-out (link, API, bounce, complaint)
  sets the recipient's status and adds the address to the suppression
  list. Unsubscribing via the email link previously skipped the list.
  RecipientService normalises addresses, so a mixed-case re-add can't
  bypass an opt-out, and complained recipients can't be re-added.
- Merge-tag values are HTML-escaped so imported data can't inject
  markup or links into emails.
@op-techy
op-techy merged commit d008410 into main Dec 4, 2026
2 checks passed
@op-techy
op-techy deleted the fix/send-compliance branch December 4, 2026 02:05
@op-techy
op-techy restored the fix/send-compliance branch December 4, 2026 02:06
@op-techy
op-techy deleted the fix/send-compliance branch December 4, 2026 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant