Email setup (operator)

Two systems send email for KolorIQ, and both must be set up for mail to reach inboxes:

Sender Messages Where it is set
The product (API and workers), through SendGrid On the list, you are in, survey ready, survey did not finish, consent confirmation Environment variables on the server
Supabase, through the same SendGrid account The sign-in code The Supabase project's Auth settings

All of them share one look, have a plain-text version, carry no images and no tracking, and are small. Those are deliberate choices for deliverability; keep them when editing.

Getting started (operator, in this order)

The domain koloriq.com already has mailboxes at Titan. Nothing below may break them.

  1. Write down the domain's current mail records (MX, SPF, DKIM, DMARC if any) before changing anything, and send a test message to an existing mailbox.

  2. Authenticate a sending subdomain in SendGrid (Settings, Sender Authentication, Authenticate Your Domain), for example mail.koloriq.com. SendGrid gives you CNAME records for that subdomain. They sit beside the Titan records and do not replace them.

  3. Do not add a second SPF record on the main domain. A domain may have only one. SendGrid's records live on the subdomain, so the Titan SPF record stays as it is.

  4. Add a DMARC record in monitoring mode on the main domain if there is none: v=DMARC1; p=none; rua=mailto:<a mailbox you read>. Read the reports for two weeks. Tighten to quarantine only when both Titan mail and SendGrid mail pass.

  5. Turn off link branding and click tracking for this sender in SendGrid if they are on by default. The product also switches tracking off on every message it sends.

  6. Create a SendGrid API key limited to "Mail Send". Set these on the server, by name:

    Name Meaning
    SENDGRID_API_KEY The key from this step. Without it the product keeps mail in memory and sends nothing
    MAIL_FROM The address mail comes from, at the main domain, for example hello@koloriq.com
    MAIL_FROM_NAME The name shown beside it. Default KolorIQ
    MAIL_REPLY_TO A Titan mailbox somebody reads. Replies go here
  7. Point Supabase at SendGrid (Authentication, Emails, SMTP Settings): host smtp.sendgrid.net, port 587, user name apikey, password the API key, sender the same address and name as above. Without this Supabase uses its own sender, which allows only a few emails an hour and looks nothing like ours.

  8. Install the sign-in templates (Authentication, Emails, Templates). For "Magic Link" paste supabase/templates/magic_link.html; for "Confirm signup" paste supabase/templates/confirmation.html. Subject for both: Your KolorIQ sign-in code. Set the email code length to 6 digits and its expiry to one hour.

  9. Set the site address and redirect addresses in Supabase (Authentication, URL Configuration) to the web app's address, so the button in the email lands on the right site.

  10. Test before telling anyone. Sign in with a Gmail address and an Outlook address. Each email should arrive in the inbox within a minute, show KolorIQ as the sender, and pass SPF, DKIM and DMARC (in Gmail: "Show original"). Then send a message from a Titan mailbox and check it still arrives.

Bounces and spam complaints (operator)

SendGrid can tell the product when a message bounced or was reported as spam. The product then stops follow-up email to that account and shows the event in the operator console, Mail tab. Set it up after step 10:

  1. In SendGrid open Settings, Mail Settings, Event Webhooks, and create a new webhook.
  2. Post URL: the API's address followed by /mail/webhook. When the API is reached through the web app that is https://koloriq.com/api/mail/webhook.
  3. Events to post: Bounced, Dropped, Spam Reports and Unsubscribes. Leave Delivered, Opened and Clicked off: the product ignores them and they are most of the volume.
  4. Switch Signature Verification on and save. SendGrid then shows a Verification Key.
  5. Set that key on the API server as MAIL_WEBHOOK_PUBLIC_KEY (the name only is written here; it is a public key, but keep it out of chat and tickets all the same) and restart the API.
  6. Press Test Your Integration in SendGrid. Its test events have no account behind them, so they appear in the Mail tab as "no account with that address". That proves the path works.
An event What the product does
Bounced (the address does not work) Recorded. No follow-up email to that account for 180 days. Service messages are still attempted; SendGrid itself drops them while the address is on its bounce list
Bounced, type blocked (refused for now) Recorded only
Dropped (SendGrid did not send, usually an earlier bounce or complaint) Recorded only
Spam report Recorded. Marketing consent is withdrawn, with the note "Reported an email as spam." No follow-up email for 180 days
Unsubscribe at SendGrid Recorded. Marketing consent is withdrawn

A bounced "survey ready" email changes nothing for the user: the result is on their study page.

Changing the look or the words

The messages are built in marketsim_api/emailer.py. After changing the sign-in wording, run

python -m marketsim_api.emailer

to regenerate the two files under supabase/templates/, commit them, and paste them into Supabase again (step 8). A test fails if the files and the code disagree.

What is recorded

Table email_log holds one row per message the product tried to send: the account, the kind (survey-ready, admitted, ...), whether the provider accepted it, and when. It never holds the address or the text. Sign-in emails are sent by Supabase and appear in its own logs and in SendGrid's activity feed.

Table mail_events holds one row per bounce, drop, spam report or provider unsubscribe: the provider's event id, the account (when one has that address), which message it was, SendGrid's reason with any email address removed, and when. It never holds the address. Rows go when the account is deleted.

Limits

  • The SendGrid sender has been tested against a stand-in only. The first real send is step 10.
  • The bounce and complaint webhook has been tested against keys made in the tests only. Its first real delivery is step 6 of "Bounces and spam complaints".
  • Follow-up email carries the one-click unsubscribe headers and a postal address; service email carries neither. See Follow-up email.
  • SendGrid keeps its own bounce and spam lists. Removing an address from them is done in SendGrid (Suppressions); the product's 180-day pause for follow-up email runs out by itself.
  • A welcome or "you are in" email the provider refuses is tried again on the worker's next pass. A "survey ready" email is tried once; the result is on the study page either way.

Troubleshooting

Symptom Cause Fix
Log line "SENDGRID_API_KEY or MAIL_FROM is not set: emails are kept in memory and not sent" One of the two variables is missing on the API or a worker Set both and restart that service
Log line "mail provider refused '...' with status 401" or 403 The API key is wrong, revoked, or lacks Mail Send Create a new key with Mail Send and update SENDGRID_API_KEY
Log line "mail provider refused '...' with status 400" MAIL_FROM is not at an authenticated domain, or the address is malformed Finish step 2, or correct the address
Sign-in emails still look plain or arrive slowly Supabase is using its own sender or default template Steps 7 and 8
Mail lands in spam The domain is new to sending, records are missing, or tracking is on Check SPF, DKIM and DMARC pass (step 10); confirm step 5; keep volume low for the first weeks
Titan mailboxes stopped receiving An MX record was changed or lost when DNS was edited or moved Restore the MX records written down in step 1
Mail from Titan mailboxes is rejected after adding DMARC The policy was tightened before Titan mail passed Set the policy back to p=none and check Titan's DKIM is set up
The Mail tab says "Bounces and spam complaints are not being received: MAIL_WEBHOOK_PUBLIC_KEY is not set." The verification key is not set on the API Steps 4 and 5 of "Bounces and spam complaints"
SendGrid's webhook test fails with 400 and "Signature check failed." The key on the server is not the one SendGrid shows for this webhook, or something between SendGrid and the API changes the body Copy the Verification Key again (step 4), restart the API. If a proxy rewrites the body, post to the API's own address instead
SendGrid's webhook test fails with 503 and "Mail events are not switched on." MAIL_WEBHOOK_PUBLIC_KEY is empty on the API that received it Step 5, then restart
SendGrid's webhook test fails with 404 The Post URL is wrong; through the web app it needs /api before /mail/webhook Step 2
A user gets no tips although they are confirmed Mail to them bounced or they reported an email as spam in the last 180 days Mail tab, find the event. If the address works again, remove it from SendGrid's Suppressions; follow-up resumes when the 180 days pass
Rows in email_log with accepted false The provider refused or could not be reached at that moment Read the worker and API logs at that time for the status