Skip to Content
DevelopersOperationsEmail sending

8 min read

Email sending and sender domains

Every store’s customer email goes out through one email account owned by the platform: Amazon SES or Resend (SENDER_EMAIL_PROVIDER), optionally with the other as a fallback (SENDER_EMAIL_FALLBACK). Each store can add its own sender address (support@waimers.in); its domain becomes a domain identity in the platform’s SES account. Merchants never need an AWS or Resend account. A store can still connect the Amazon SES app with its own keys; then that app sends instead and none of this applies.

Providers

ProviderPackageSendsStore sender domains
Amazon SESpackages/notifications-ses (AWS SDK v2 client)Platform address and stores’ verified domainsYes, SES domain identities
Resendpackages/notifications-resend (fetch to POST https://api.resend.com/emails, no SDK)Platform address onlyNo (see below)

Both channels carry the same things: From name (cleaned, quoted or MIME encoded-word by formatMailbox), Reply-To, attachments (Resend: base64 with content_type) and, on marketing sends, the List-Unsubscribe headers. Resend sends also carry a tag: template=<id> for transactional mail, kind=marketing for campaigns.

Why store domains stay on SES. Resend’s Domains API could verify a domain too, but its records are different (a resend._domainkey TXT key plus MX/SPF on a send. subdomain, not three SES DKIM CNAMEs), so the sender_email table, the DNS record builder and the Sender email card would all need a second shape, and with a fallback the merchant would have to add both sets of records. So a verified store domain always sends through SES, whatever SENDER_EMAIL_PROVIDER says; Resend sends only PLATFORM_EMAIL_FROM. Without SES keys (resend alone) the Sender email card cannot start a domain (it shows the provider error), and any store with a verified domain from before sends from the platform address with Reply-To the shop.

Fallback

withFallback (packages/notifications/src/fallback.ts) wraps two channels: the second is tried only when the first fails with retryable: true on its SendResult, i.e. the provider, not the message, failed:

  • SES (packages/notifications-ses/src/ses.ts): the SDK’s $retryable or $fault: server, HTTP 429/5xx, Throttling, TooManyRequests, LimitExceeded, SendingPaused, AccountSuspended, service errors, socket errors (ECONNRESET, ETIMEDOUT, …).
  • Resend: no answer (network, 15 s timeout), HTTP 429 (rate limit, daily or monthly quota) or 5xx.

A rejected message (bad address, validation, unverified From) is never sent twice. The notification log keeps the platform_email channel id either way; when the fallback delivered, the provider reference is prefixed resend: or ses:. When SES sends a store’s own domain and fails, the Resend fallback sends from the platform address with Reply-To the shop, as before verification. A timeout can, rarely, mean the first provider did deliver, so a fallback may duplicate that one email.

SENDER_EMAIL_PROVIDERSENDER_EMAIL_FALLBACKPlatform addressVerified store domain
sesnoneSESSES
sesresendSES, then ResendSES, then Resend from the platform address
resendnoneResendSES if its keys are set, else Resend from the platform address
resendsesResend, then SESSES, then Resend from the platform address

Pieces

PieceWhereWhat it does
sender_email tablepackages/db/src/schema.ts, migration 0067_sender_emailOne row per store (unique store_id), domain unique across the platform, status pending | verified | failed, DKIM tokens, MAIL FROM state. Under RLS.
sender_domain_taken(domain, store)SQL, SECURITY DEFINERTrue when another store holds the domain, a parent or a subdomain. Returns yes/no only.
stores_with_sender_email_checks()SQL, SECURITY DEFINERStores with a pending domain, or a verified one not checked for a day. Feeds the worker.
SesIdentityProviderpackages/notifications-ses/src/identity.tsCreateEmailIdentity (Easy DKIM), GetEmailIdentity, PutEmailIdentityMailFromAttributes, DeleteEmailIdentity.
Dev providerpackages/core/src/sender-email/dev-provider.tsOffline: *.test, *.example, *.localhost verify on the first check, fail.* fails, others stay pending.
Rulespackages/core/src/sender-email/client.ts (@commerce/core/sender-email)Address parsing, DNS record builder, chooseSender. Pure, safe in the browser.
Lifecyclepackages/core/src/sender-email/service.tssetSenderEmail, checkSenderEmail, removeSenderEmail, pollSenderEmails.
Channelspackages/core/src/sender-email/channels.tsstoreNotificationChannels: enabled apps plus the platform sender. Every send path uses it.
APIpackages/api/src/routers/notifications.tssenderEmail, setSenderEmail, checkSenderEmail, removeSenderEmail.
Workerapps/worker/src/sender-email.tsRepeat job every 5 minutes on {sender-email}-verify.
UIapps/admin/src/components/notifications/sender-email-card.tsxThe Sender email card on Settings → Notifications.

The From / Reply-To rule

chooseSender (packages/core/src/sender-email/client.ts) decides, for every email:

  • Verified sender, address on that exact domain: From: Store name <support@waimers.in>, no Reply-To.
  • Anything else (no sender, pending, failed): From: Store name <PLATFORM_EMAIL_FROM>, Reply-To: support@waimers.in (or the store’s contact email when no sender is set).

The display name is the sender name, else the store name. It is cleaned (no CR/LF or angle brackets), quoted when needed and MIME encoded-word for non-ASCII (formatMailbox, packages/notifications-ses/src/address.ts).

storeNotificationChannels adds the platform channel (id platform_email) only when the platform account is configured and the store has no email app of its own (the dev log channel does not count). deliverNotification, deliverCartRecovery, deliverLoginCode, gift card delivery, marketing sends, back-in-stock alerts and Send me a test all resolve channels through it.

List-Unsubscribe / List-Unsubscribe-Post ride only on sendMarketing (campaigns and automations); transactional send never sets them.

Lifecycle

  • Same domain, new local part or name: the identity stays; the row is re-checked.
  • New domain: the claim is checked (sender_domain_taken), a new identity is created, the row replaced in a savepoint, then the old identity deleted.
  • Race: two stores claiming at once hit the domain unique index; the loser gets domain_taken and the winner’s identity is left alone.
  • MAIL FROM is bounce.<domain> with BehaviorOnMxFailure: USE_DEFAULT_VALUE, so a missing MX never blocks mail. If SES refuses it on a pending identity, it is retried once DKIM passes.

Security

  • Only a verified domain goes in From, and only for an address on that domain.
  • Addresses are validated (parseSenderAddress): format, free-mail providers, platform domains (PLATFORM_DOMAINS and the domain of PLATFORM_EMAIL_FROM, including parents and subdomains).
  • One store, one domain. Parents and subdomains are blocked too, because SES lets a subdomain inherit its parent’s verification.
  • Rate limits: setSenderEmail 5 per hour per store (each may create an SES identity), checkSenderEmail 12 per 10 minutes.
  • SETTINGS_MANAGE for changes, SETTINGS_READ to view.

Env

KeyDefaultMeaning
SENDER_EMAIL_PROVIDERdevses or resend turns on the platform account and picks who sends the platform address. dev checks domains offline and sends nothing through the platform.
SENDER_EMAIL_FALLBACKnone (blank = none)ses or resend: the other provider, tried on provider errors. Needs that provider’s keys too. Naming the provider itself is ignored.
RESEND_API_KEYnoneResend API key with sending access. PLATFORM_EMAIL_FROM’s domain must be verified in the Resend dashboard (once, by hand).
SES_REGIONnoneRegion of the platform SES account, e.g. ap-south-1. Also the MAIL FROM MX (feedback-smtp.<region>.amazonses.com).
SES_ACCESS_KEY_ID, SES_SECRET_ACCESS_KEYnoneIAM user with ses:SendEmail, ses:CreateEmailIdentity, ses:GetEmailIdentity, ses:DeleteEmailIdentity, ses:PutEmailIdentityMailFromAttributes, ses:TagResource.
PLATFORM_EMAIL_FROMnoneThe platform’s verified sender, e.g. no-reply@mail.waimers.com. Verify its domain in SES by hand once.
PLATFORM_EMAIL_NAMEthe platform name (siteConfig.brand.name)Display name when a store has no name.
SES_CONFIGURATION_SETnoneConfiguration set every store identity uses by default and every platform SES send names (ConfigurationSetName). Its event destination sends bounces and complaints to SNS. It must exist in SES, or SES refuses every send.
SES_EVENTS_TOPIC_ARNnoneARN of that SNS topic. The worker’s POST /webhooks/email/ses accepts only messages from this exact topic; unset, it refuses everything. Worker only.

Set these on all three apps (admin, storefront, worker): the storefront sends sign-in codes, the worker sends order email and runs the poll, the admin creates identities. SENDER_EMAIL_PROVIDER=ses without the SES keys or PLATFORM_EMAIL_FROM (or resend without RESEND_API_KEY, or a fallback without its keys) fails at first use with a clear error. Blank values (compose’s ${KEY:-}) count as unset. The SES account must be out of the sandbox (production access), or it can only mail verified addresses. The same SES_* keys drive the SES package’s live test suite, so never put real keys in .env.test.

Bounces and complaints

Each store keeps its own suppression list (email_suppression, migration 0240). SES reports a bounce or complaint to an SNS topic; SNS posts it to the worker; the store stops mailing that address until staff press Allow again.

  • Sending. platformEmailFromEnv names SES_CONFIGURATION_SET on every SES send and tags it store-id=<store> (transport.channel(sender, { storeId })), for the platform address and a store’s verified domain alike.
  • Receiving (packages/core/src/email-suppression/, route apps/worker/src/email-feedback.ts): the body must be an SNS envelope (raw message delivery off) from exactly SES_EVENTS_TOPIC_ARN; the signature (SignatureVersion 1 SHA1 or 2 SHA256) is checked against the certificate at SigningCertURL, fetched only from https://sns.<region>.amazonaws.com/…pem (cached per process). A SubscriptionConfirmation that passes is confirmed by visiting its SubscribeURL, only on an SNS host with Action=ConfirmSubscription. Refusals get the generic webhook answer; a database error is a 500 so SNS retries.
  • Store. The store-id tag when that store exists (store_for_email_tag), else the store whose sender domain sent it (store_for_sender_domain); neither: ignored.
  • Rules (recordEmailFeedback): Permanent bounce or complaint, suppressed at once. Transient/Undetermined bounces: suppressed after SOFT_BOUNCE_LIMIT (3) within SOFT_BOUNCE_WINDOW_DAYS (14), counting only bounces after the last Allow again. Every event is a row in email_feedback_event, unique per store, SNS MessageId and address, so a redelivery is a no-op.
  • Skipping. storeNotificationChannels loads the store’s active list once and wraps every email channel (withSuppressionGuard): send and sendMarketing to a suppressed address return { ok: false, error: 'suppressed: <reason>: …' } without calling the provider, so the notification / marketing message row records why. That covers order messages, one-off emails, gift cards, campaigns and automations. Sign-in codes and links (login_code, login_link) still go out. Platform-to-merchant mail (billing, staff invites) does not use a store’s channels and is not checked.
  • Admin. Customer page notice (customerPrivacy.emailSuppression, customerPrivacy.allowEmailAgain); Settings › Notifications › Sender email list (notifications.emailSuppressions, notifications.allowSuppressedEmail).
  • Erasure deletes the customer’s rows in both tables.
  • Not covered: a store’s own SES app (bounces go to its own AWS account) and Resend (no webhook yet). SES’s account-level suppression list stays on as a second net.

AWS setup (once, by the platform owner)

  1. SNS topic. In the SES region (e.g. ap-south-1), create a Standard topic, e.g. ses-events. Copy its ARN.
  2. Worker env. Set SES_EVENTS_TOPIC_ARN to that ARN on the worker and redeploy it, before step 3, or the confirmation is refused.
  3. Subscription. On the topic, create an HTTPS subscription to ${WORKER_URL}/webhooks/email/ses. Leave raw message delivery off. The worker confirms it on its own; the subscription shows Confirmed within a minute.
  4. Configuration set. In SES › Configuration sets, create one (e.g. koomerce-events) and add an event destination: Amazon SNS, the topic above, event types Hard bounces and Complaints (add Delivery delays only if you want; they are ignored). If AWS asks, let SES publish to the topic.
  5. Sending env. Set SES_CONFIGURATION_SET to its name on admin, storefront and worker, and redeploy. Every platform SES send names it from then on.
  6. Check. In a test store, add a customer with the email bounce@simulator.amazonses.com (SES mailbox simulator) and use Email customer. Within a minute the address shows under that store’s Settings › Notifications › Sender email list and on the customer page. Allow again it afterwards. complaint@simulator.amazonses.com does the same for a complaint.

Keep the SES account-level suppression list on.

Testing

All SES calls are mocked: packages/notifications-ses/test/identity.test.ts (SDK commands and state mapping), packages/core/src/sender-email/client.test.ts (rules) and packages/core/src/sender-email/service.test.ts (lifecycle, claims across stores, RLS, the From/Reply-To choice before and after verification, BYO SES precedence).

Resend is never called for real: packages/notifications-resend/test/resend.test.ts mocks fetch (request body, error mapping, retryable classes, conformance suite). packages/notifications/test/fallback.test.ts covers withFallback, and packages/core/src/sender-email/platform.test.ts covers provider choice, fallback both ways and store domains staying on SES, with SES and Resend both mocked.

Was this helpful?
Last updated on