Tracking and unsubscribe
Karta can record when a recipient opens a message (an image is loaded) and clicks a link, and can add a one-click unsubscribe link (RFC 8058). All three are off until a server turns them on, each on its own:
{:ok, server} =
Karta.Servers.update_server(server, %{track_opens: true, track_clicks: true, list_unsubscribe: true})They also need the tracking host: without KARTA_TRACKING_URL (or a
tracking host of the sender's domain)
nothing is tracked and no List-Unsubscribe is added, whatever the server
says.
The tracking host
The tracking host is a second HTTP endpoint (KartaWeb.TrackingEndpoint)
on KARTA_TRACKING_PORT (default 4001), which the reverse proxy serves
under its own name, KARTA_TRACKING_URL (for example
https://track.example.net). It serves only:
| Path | What it does |
|---|---|
GET /o/<token>.gif | records an open; answers a 1x1 transparent GIF |
GET /c/<token> | records a click; redirects (302) to the link's URL as it was sent |
GET /u/<token> | a page with an "Unsubscribe" button; changes nothing by itself |
POST /u/<token> | one-click unsubscribe |
GET /.well-known/mta-sts.txt | at mta-sts.<domain>: the MTA-STS policy Karta serves for the domain (Hosting an MTA-STS policy) |
GET /bimi/<sha256>.svg | a verified domain's BIMI logo (BIMI logo) |
GET /tls/ask?domain=<name> | for the reverse proxy: whether it may get a certificate for the name (below) |
Everything else, and every token that does not verify or whose message is
gone, is the same plain 404 Not Found. The APIs are not on this host, and
these paths are not on the API host. It parses no request bodies, sets no
cookies and keeps no sessions. Every answer has Cache-Control: no-store,
Referrer-Policy: no-referrer (the destination of a click never sees the
token), X-Robots-Tag: noindex, nofollow, X-Content-Type-Options: nosniff
and a content security policy that allows no scripts.
Tracking hosts of sending domains
By default one tracking host serves every organisation, so the links in
all customers' mail point at one name, unrelated to the sender, and share
its reputation. A verified domain can have a tracking host of its own
instead, such as click.example.com for example.com:
Add it on the domain's page (owners and admins), or from the console:
{:ok, host} = Karta.Tracking.Hosts.add(domain, %{name: "click"})Publish the CNAME record the page shows, to Karta's tracking host:
click.example.com. 3600 IN CNAME track.example.net.The target is
KARTA_TRACKING_CNAME, by default the host ofKARTA_TRACKING_URL.Karta looks the record up when the host is added, when someone presses Check DNS (
Karta.Tracking.Hosts.check/2), and every hour. The host is verified while the record is there and points to the target. A record that is missing or points elsewhere withdraws verification, and the host is no longer used; a lookup that fails changes nothing.
The tracking URL of a message is decided at each delivery run:
- the tracking host of its author's domain (the first From address), while that host and the domain are verified;
- else the tracking host its server uses (Tracking host in the
server's settings,
Karta.Tracking.Hosts.assign_server/2): one of its organisation's hosts, for mail from domains without one of their own; - else
KARTA_TRACKING_URL.
The links, the open pixel and the List-Unsubscribe URL of each copy all
use it, always over https. Tokens do not depend on the host, so links
already sent keep working after a host changes, as long as their name
still reaches Karta. A tracking host is a name under its domain
(click.example.com, t.mail.example.com); a domain has at most one, a
name belongs to one domain in the instance, and it is added only to a
verified domain, so only the organisation that verified a domain can claim
names under it. It is deleted with its domain.
Certificates for tracking hosts
Each tracking host needs a valid certificate on the reverse proxy, which
sends it to KARTA_TRACKING_PORT like the tracking URL's host. There are
two ways to get one for every host without configuring each by hand.
On-demand TLS (Caddy). The proxy gets a certificate for a name the
first time a client asks for it, after asking Karta whether the name is
one of its own: GET /tls/ask?domain=<name> on the tracking endpoint
answers 200 for the host of KARTA_TRACKING_URL, for verified
tracking hosts of verified domains and for the mta-sts.<domain> hosts of
the MTA-STS policies Karta serves (their CNAME record found), and 404
for anything else, so the
proxy never requests certificates for names that others point at it. The
question is not redirected to HTTPS, so the proxy can ask over its
internal network:
{
on_demand_tls {
ask http://karta:4001/tls/ask
}
}
https:// {
tls {
on_demand
}
reverse_proxy karta:4001
}Traefik. Traefik does not ask; instead Karta writes a dynamic
configuration file for each verified tracking host, and each policy host
in use, into a directory that
Traefik's file provider watches (KARTA_TRACKING_TRAEFIK_DIR): a router
for the host name to the service of the tracking endpoint, with a
certificate from a certificate resolver.
# karta-tracking-click.example.com.yml
http:
routers:
karta-tracking-click_example_com:
rule: "Host(`click.example.com`)"
service: "karta-tracking@docker"
entryPoints:
- "websecure"
tls:
certResolver: "letsencrypt"Each node writes its own directory when it starts and then every minute
(Karta.Tracking.Traefik): a file only when its content changed (under a
temporary name, then renamed), the files of hosts no longer in use
removed, other files left alone. Set KARTA_TRACKING_TRAEFIK_SERVICE to
the Traefik service that reaches the tracking endpoint (such as the one
the container's labels define, karta-tracking@docker),
KARTA_TRACKING_TRAEFIK_CERT_RESOLVER to the certificate resolver and,
if needed, KARTA_TRACKING_TRAEFIK_ENTRYPOINTS; and let Traefik's file
provider watch the directory:
# Traefik's static configuration
providers:
file:
directory: /etc/traefik/dynamic
watch: trueWhat changes in the message
At delivery, for each recipient (Karta.Tracking.Rewriter):
- Clicks: every trackable link in the HTML is replaced by
<tracking URL>/c/<token>. A link is trackable when it is thehrefof anaorareatag and is an absolutehttporhttpsURL with a host, printable ASCII only, at most 2048 bytes.mailto:,tel:, relative links, fragments andjavascript:links are never changed, nor links in comments (including Outlook conditional comments), inscript,styleand similar elements, or to the tracking host itself. At most 1000 distinct links per message are tracked. - Opens: an image
<tracking URL>/o/<token>.gifis inserted before the last</body>(or at the end of the HTML). - Unsubscribe:
List-Unsubscribe: <<tracking URL>/u/<token>>and, for an https URL,List-Unsubscribe-Post: List-Unsubscribe=One-Clickare added, unless the message has its ownList-Unsubscribe.
Only HTML text parts change: text/html parts that are not attachments, in
an ASCII-compatible charset, with any standard transfer encoding.
Plain-text parts, attachments and attached messages are never changed, and
a message that cannot be parsed keeps its body. Every other byte of the
HTML stays as it was; the document is not re-serialised. The changed copy
is DKIM-signed afterwards, so the signature covers the tracking.
As in Postal, a link written https+notrack://... (or http+notrack://)
is not tracked and is sent as https://....
Tracking does not apply to incoming mail or delivery status notifications. A message can switch opens or clicks off for itself (Tags, variables and scheduling), never on.
Tokens
A token names one recipient (and, for a click, the link's position in the message) and is signed: HMAC-SHA256, truncated to 128 bits, with a key derived from the vault key. The kind of token is part of the signature, so the open pixel (which image proxies see) cannot be used as an unsubscribe link. Nothing is looked up before a token verifies. A click always redirects to the URL stored when the message was sent; nothing in the request can change the target, and Karta never fetches it.
Tokens made under a vault key keep working after the key is rotated, as long as it is kept among the retired keys.
What is recorded
Each open or click (Karta.Tracking.Event) stores:
- the recipient and, for a click, the link;
- the client's network, not its address: an IPv4 address with the last
byte zeroed (
/24), the first 48 bits of an IPv6 address; - the user agent, at most 256 printable ASCII characters.
At most 60 events per recipient per minute are recorded (more are served but not stored), and only while the server still tracks that kind. Events are deleted with their message by retention.
Behind a reverse proxy, the client is the one the proxy names in
X-Forwarded-For, when the proxy is listed in KARTA_TRUSTED_PROXIES;
otherwise the network recorded is the proxy's.
Image proxies and mail scanners load images and follow links on the recipient's behalf, so opens and clicks are indications, not proof.
Events are listed by GET /api/v2/messages/{id}/events, counted per
recipient in GET /api/v2/messages/{id} (opens, clicks), returned as
activity_entries by the Postal API and as opened and clicked events
by Mailgun's events API. Each recorded event is also sent to the server's
webhooks that want it (message.opened,
message.clicked).
Unsubscribing
GET /u/<token> shows a page that asks before unsubscribing, because mail
scanners and link previews follow links; RFC 8058 lets only the POST
unsubscribe. POST /u/<token> (what mail clients send for one-click
unsubscribe, and what the page's button sends) puts the recipient's address
on the server's suppression list with the reason unsubscribe and the
source list-unsubscribe, once: repeating it changes nothing, and an
address already on the list for another reason keeps that reason. Neither
page names the address. The first unsubscribe is sent to the server's
webhooks as message.unsubscribed.
Because the suppression applies to all of the server's mail, turn
list_unsubscribe on for servers that send newsletters, not for servers
that send receipts or password resets.