---
title: "Let people choose their emails"
description: "Declare email topics on setupEmail, confirm newsletter topics with double opt-in, add paying customers under the EU soft opt-in, give every marketing email RFC 8058 one-click unsubscribe, cap how often people hear from you, keep quiet hours, sunset inactive readers, and let people change their minds with useEmailPreferences."
canonical_url: "https://nuxt-backend.dev/docs/build/email/preferences"
---
# Let people choose their emails

> Declare email topics on setupEmail, confirm newsletter topics with double opt-in, add paying customers under the EU soft opt-in, give every marketing email RFC 8058 one-click unsubscribe, cap how often people hear from you, keep quiet hours, sunset inactive readers, and let people change their minds with useEmailPreferences.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- **You get:** topics people join on purpose, one-click unsubscribe in every marketing email, a preference center, and sending rules that keep your list healthy: a weekly cap, quiet hours and a sunset for readers who stopped reading.
- **Powered by:** Resend's topics, which keep each contact's choices, honor them in broadcasts and report every change, and its hosted unsubscribe page.
- **The package adds:** topics as code, double opt-in, the soft opt-in at checkout, a copy of the choices on your deployment, the RFC 8058 route and page for your own sends, the consent record, and the sending rules.

Let each person decide which emails they get: product news yes, tips no. You declare the topics your app writes about. Every email sent under a topic checks the person's choice and the sending rules, and carries a one-click unsubscribe. People change their minds on a page the package ships, or on their profile.

Transactional email is never held to these rules: a sign-in code, an invitation or a receipt goes out whatever a person chose.

## Declare the topics

Topics go on `setupEmail`, keyed by the name your code sends by:

```ts [backend/email.ts]
export const email = setupEmail(components, {
  topics: {
    productNews: {
      name: 'Product news',
      description: 'New features and launches, about once a month.',
      subscribedByDefault: false,
      doubleOptIn: true,
      softOptIn: true,
    },
    stripTips: {
      name: 'Strip tips',
      description: 'Ideas for your next strip, every other week.',
      subscribedByDefault: false,
      from: 'Panels Tips <tips@news.panels.app>',
    },
  },
})
```

- **name and description** are what people read on the unsubscribe page and their profile. Resend caps them at 50 and 200 characters.
- **subscribedByDefault** says whether people are in the topic until they leave. Keep it `false` for marketing, so people join on purpose. Choose once: Resend can't change a topic's default after it creates the topic. Resend's own names read backwards to some: its `opt_in` default means everyone is in, and `opt_out` means nobody is until they join. `subscribedByDefault: false` creates an `opt_out` topic.
- **doubleOptIn** asks for a confirmation click before a person joins ([Double opt-in](#double-opt-in)).
- **softOptIn** adds paying customers unless they object at checkout ([The soft opt-in at checkout](#the-soft-opt-in-at-checkout)).
- **from** is the sender for the topic's email. Without it, `EMAIL_MARKETING_FROM`, then `EMAIL_FROM` ([Two senders](https://nuxt-backend.dev/docs/build/email/sending#two-senders)).

Then create them at Resend:

```bash
npx nuxt-backend email sync
```

`email sync` does for email what `billing sync` does for the catalog. It creates the topics, segments and contact properties `backend/email.ts` declares, and subscribes the deployment's webhook to the events they need. It changes nothing that is already right, so run it again after each change. Their ids stay on the deployment, so no provider id sits in your code. It needs a full-access `EMAIL_API_KEY`: a key restricted to sending can't manage topics. `doctor` reports the topics Resend is missing (`email-topics`), and `doctor --fix` runs `email sync` for you, once you confirm it (or pass `--yes`).

Once a topic exists, the package keeps each account as a Resend contact, in one segment for your app's accounts. When the address changes, the choices move to the new address, the very next send reads them there, and the old address's contact is removed. The contact is removed when the account is erased.

## Send under a topic

Pass the topic's key with the send:

```ts [backend/digest.ts]
await email.send(ctx, {
  to: user.email,
  subject: 'Three strip ideas for this week',
  html,
  topic: 'stripTips',
})
```

A send under a topic first reads the recipient's choice from the deployment's copy, then the sending rules below. A person who isn't in the topic gets nothing: `send` resolves to `null` and logs `email.skipped_unsubscribed`. Everyone else gets the email from the marketing sender, with your postal address in the footer ([Brand your emails](https://nuxt-backend.dev/docs/build/email/templates/theme#the-postal-address)) and the unsubscribe headers below. A [lifecycle email](https://nuxt-backend.dev/docs/build/growth/engage/lifecycle-emails) and an [announcement](https://nuxt-backend.dev/docs/build/growth/engage/announcements) take a topic the same way. A billing event starts a lifecycle or marketing email only for people who opted into its topic: a topic that is on by default doesn't count, and receipts stay Polar's.

## Double opt-in

A topic with `doubleOptIn: true` asks people to confirm before they join. Ticking the topic on the profile sends a confirmation email with one button; the person joins once they press it, and their choice reads `pending` until then. The confirmation link is signed, works once and lapses after 7 days. Asking again sends a fresh link, at most once in 5 minutes, however often the topic is ticked.

A sign-in code already proved the address, so the click proves something else: that the person asked for this mail. Some EU courts expect that proof for a newsletter, and it keeps typo'd and borrowed addresses off your list. The consent ledger records the click with the topic's description at that moment ([Consent](#consent)).

Use it for newsletters and anything promotional. A topic about the person's own use of your app, such as strip tips, can skip it.

## The soft opt-in at checkout

In the EU, ePrivacy Art. 13(2) lets you email a customer about your own similar products when you got their address in a sale. You must offer them a clear, free way to object when you collect the address and in every email after. A topic with `softOptIn: true` does both:

- **At checkout,** `<PricingTable>` and `<BillingCheckoutLink>` show one line under the button, with an unticked box: "We'll email you about new features. Don't email me." The box is the objection; leaving it unticked is not consent to anything else.
- **When the order is paid,** the customer joins the topic unless they ticked the box. The consent ledger records it as a soft opt-in, with the checkout it came from.
- **Every email under the topic** carries the one-click unsubscribe, as all topic mail does.

The soft opt-in covers similar products only, and people who bought. A free sign-up is not a sale. Outside the EU the rules differ, and this is not legal advice: see [Follow email law](https://nuxt-backend.dev/docs/ship/compliance/email-law).

## Cap how often people hear from you

`caps` on `setupEmail` sets the rules every send under a topic follows, together with lifecycle emails and the email and push channels of [notifications](https://nuxt-backend.dev/docs/build/email/notifications):

```ts [backend/email.ts]
export const email = setupEmail(components, {
  // …your topics
  caps: {
    perWeek: 3,
    quietHours: { start: '21:00', end: '08:00' },
    sunsetAfterDays: 180,
  },
})
```

- **perWeek** is the most non-transactional emails one person gets in any 7 days, across every topic. A send past it is skipped and logged `email.skipped_capped`; a lifecycle flow moves on to its next step. It takes 1 to 50. No default: leave it out and nothing is capped.
- **quietHours** is a window in each person's own time zone, which the app records from their browser at sign-in. A send that would land inside it is scheduled for its end on the person's own clock, also on a night the clocks change. It checks the topic again then: someone who left it meanwhile, or was erased, gets nothing. A push notification waits the same way. People with no recorded time zone use UTC. No default.

A send that is skipped or held is still a decision you can see: the log line names the rule, never the address.

Neither rule touches a transactional email: one you send without a topic, or a notification kind marked `transactional`, such as the package's security alerts ([Send what people wait for at once](https://nuxt-backend.dev/docs/build/email/notifications/custom-events#send-what-people-wait-for-at-once)).

## Sunset inactive readers

`sunsetAfterDays`, 180 by default, stops non-transactional mail to anyone with no click in your email and no activity in your app for that long. They stay subscribed: the first click or sign-in brings them back. A send to them resolves to `null` and logs `email.skipped_sunset`.

Engagement counts clicks and app activity, never opens. Apple Mail Privacy Protection loads every image on arrival, so an open says nothing about a reader. Mail to people who stopped reading drives complaints up, and complaints are what [pause sending](https://nuxt-backend.dev/docs/build/email/delivery/deliverability#keep-marketing-from-stopping-sign-in) for your sign-in codes too.

## One-click unsubscribe

Every email under a topic carries the two headers of RFC 8058, which Gmail and Yahoo require from bulk senders and mail clients turn into an unsubscribe button:

```text
List-Unsubscribe: <https://<slug>.convex.site/email/unsubscribe?token=…>
List-Unsubscribe-Post: List-Unsubscribe=One-Click
```

The `/email/unsubscribe` route sits beside `/email/events`, mounted by [`registerBackendRoutes`](https://nuxt-backend.dev/docs/ship/security/webhooks). Resend adds no one-click unsubscribe to emails sent through its API, so the package owns this route. The token in it is safe to hand out:

- **Signed.** It is an HMAC with a key derived from `AUTH_SECRET`, for one contact and one topic. It can only take that contact out: out of that topic, or out of every topic. It never expires, so a link in an old email still works, and a version in it lets the secret rotate.
- **Safe to open.** A `GET` changes nothing, since mail scanners open every link they see. It sends the reader to your app's unsubscribe page, which asks before it acts.
- **One click from the mail client.** The `POST` the mail client's button sends unsubscribes at once and answers `200` with an empty body. A token that names no contact, or one that is gone, answers `200` too, so the status reveals nothing about an address.
- **Rate-limited per token.** Mail providers send one-click requests from shared addresses, so the limit counts tokens, never IP addresses.

The choice takes effect on the deployment at once and reaches Resend's contact right after, well inside the 48 hours Gmail allows.

## The unsubscribe page

The package mounts `/unsubscribe`, the page the link opens. It names the topic the email came from and asks for one press to leave it, with a second button to leave every topic. The token is enough to open it, so it works for people who are signed out or have no account, such as a waitlist. Joining a topic takes a sign-in, so a forwarded email can't sign its owner up for more mail. Like every private package page, it is never cached and never indexed.

Move it with `backend.pages.unsubscribe` in `nuxt.config`, or replace it with an app page at the same path. `useEmailLink()` is the page without its markup, for both an unsubscribe link (`?token=`) and a double opt-in's confirmation link (`?confirm=`):

```vue [app/pages/unsubscribe.vue]
<script setup lang="ts">
const route = useRoute()
const { state, topicName, leave, confirm } = useEmailLink(() => ({
  token: route.query.token as string | undefined,
  confirm: route.query.confirm as string | undefined,
}))
</script>

<template>
  <p v-if="state === 'invalid'">This link does not work anymore.</p>
  <template v-else-if="state === 'ready'">
    <button v-if="route.query.confirm" @click="confirm()">Yes, send them</button>
    <template v-else>
      <button @click="leave()">Leave {{ topicName }}</button>
      <button @click="leave(true)">Leave every topic</button>
    </template>
  </template>
  <p v-else-if="state === 'done' || state === 'done-all' || state === 'confirmed'">Done.</p>
</template>
```

Opening the page changes nothing, so a mail scanner that follows the link unsubscribes nobody. `state` also reads `loading`, `pending`, `nothing` (a confirmation with nothing left to confirm), `error` and `no-token`. `link` holds what the link names, and `announcement` says the outcome in the package's words, for a polite status.

## Preferences on the profile

A signed-in person manages the same choices in the **Email** section of `/profile`, beside their notification channels. Build your own with `useEmailPreferences()`, which reads the person's topics and channels and sets each one, or all of them at once:

```vue [app/pages/settings/email.vue]
<script setup lang="ts">
const { topics, set, unsubscribeAll } = useEmailPreferences()
</script>

<template>
  <label v-for="topic in topics" :key="topic.key">
    <input type="checkbox" :checked="topic.subscribed" @change="set(topic.key, !topic.subscribed)">
    {{ topic.name }}
    <span v-if="topic.pending">Check your inbox to confirm.</span>
  </label>
  <button @click="unsubscribeAll">Unsubscribe from all</button>
</template>
```

Choices made anywhere stay in step: on the unsubscribe page, on the profile, in Resend's own hosted page, or through its API. Resend reports each change as `contact.topics.updated` to `/email/events`, and the deployment's copy follows. "Unsubscribe from everything" on Resend's page arrives as `contact.updated`, and takes the person out of every topic. An event that arrives late never undoes a choice made after it.

## Consent

Joining a topic that is off by default is a consent: the package records it on the account's consent ledger, with the version of the topic's description the person agreed to and how they joined: a click, a confirmed double opt-in, or a soft opt-in at checkout. Leaving the topic withdraws it. The ledger joins the account's export and erasure ([Record consents](https://nuxt-backend.dev/docs/build/auth/accounts/consents)).

## Notification channels

The profile's **Email** section also holds each person's notification channels: for each kind of [notification](https://nuxt-backend.dev/docs/build/email/notifications), whether it reaches them in the app, by email or as a [push notification](https://nuxt-backend.dev/docs/build/email/notifications/web-push). Every channel a kind allows is on until the person turns it off. Billing kinds stay in the app, and a kind's email or push can only be turned on where the kind allows it ([The channel matrix](https://nuxt-backend.dev/docs/build/email/notifications#the-channel-matrix)).

`useEmailPreferences()` reads them as `channels`, each kind with the channels it allows, and `set` turns one on or off:

```ts
const { channels, set } = useEmailPreferences()
await set({ kind: 'auth.passkey_added', channel: 'email' }, false)
```

They come from `getNotificationChannels` and `setNotificationChannel` in `backend/notifications.ts` ([Set it up](https://nuxt-backend.dev/docs/build/email/notifications#set-it-up)). The list holds the kinds your app sends: a gift, plan pass, reverse trial or referral kind joins it once the deployment has used that feature, and `setupNotifications({ kinds })` names the package's kinds exactly, say to leave the workspace kinds out of an app that never shares a workspace ([List only what your app sends](https://nuxt-backend.dev/docs/build/email/notifications#list-only-what-your-app-sends)). A choice is erased and exported with the account.

## Check the setup

`npx nuxt-backend doctor` adds these findings with the feature:

- `email-topics`: the topics exist at Resend, as `email sync` creates them.
- `email-webhook-events`: the Resend webhook sends the `contact.*` and `suppression.*` events the deployment's copies need.
- `email-key-permission`: the key is full-access, as topics, contacts and broadcasts need.

`doctor --prod` also fails when topics are declared and the brand has no postal address, and warns when `EMAIL_MARKETING_FROM` is unset.

## Test it

Send yourself the whole loop on your dev deployment. Sign in as `delivered+news@resend.dev`, tick **Product news** on `/profile`, and read the confirmation in the [dev inbox](https://nuxt-backend.dev/docs/develop/dev-inbox). Confirm it, send an email under the topic, then press the unsubscribe link it carries: the profile shows the topic off, and the next send resolves to `null`.

## Related

- [Resend](https://nuxt-backend.dev/docs/services/resend): the full-access key and the webhook events topics need.
- [Announce product news](https://nuxt-backend.dev/docs/build/growth/engage/announcements): a broadcast to the people who opted in.
- [Send lifecycle emails](https://nuxt-backend.dev/docs/build/growth/engage/lifecycle-emails): the flows the caps and the sunset rule apply to.
- [Follow email law](https://nuxt-backend.dev/docs/ship/compliance/email-law): consent, the soft opt-in and the postal address, by country.
- [Get email into inboxes](https://nuxt-backend.dev/docs/build/email/delivery/deliverability#keep-marketing-from-stopping-sign-in): why consent protects sign-in too.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
