Local development
This page runs Karta on your machine with the development stack, creates an organisation, a mail server, a verified domain and an API key, and sends a first message that you can read in Mailpit. Nothing here sends mail to the internet: the development resolver knows only test zones, and the message goes to the local Mailpit.
Requirements
- Erlang/OTP 29.1.2 and Elixir 1.20.5, as pinned in
.tool-versions. With mise (or asdf) installed, runmise installin the repository. - Docker with Compose.
curlfor the examples, and Python 3 if you want to try SMTP from a script.
Start the development stack
docker compose -f dev/compose.yml up -d
dev/garage-bootstrap.sh # once per fresh Garage container
The stack (dev/compose.yml, project name karta-dev) runs:
| Service | Host port | Purpose |
|---|---|---|
| postgres | 55433 | databases karta_dev and karta_test (user and password postgres) |
| unbound | 55353 (UDP and TCP) | validating resolver that knows only the karta.test and karta-insecure.test zones |
| mailpit | 58025 (web UI and API), 51025 (SMTP) | catches the mail Karta delivers |
| rspamd | 51333, 51334 | content scanning of received mail (when configured) |
| clamav | 53310 | virus scanning of received mail (when configured) |
| garage | 53900 | S3-compatible store for message bodies |
dev/garage-bootstrap.sh creates the buckets karta-dev and karta-test
and an access key with test-only credentials; running it again changes
nothing. config/dev.exs already points Karta at all of these.
Set up and start Karta
mix setup # deps, database, assets
iex -S mix phx.server
Karta now listens on:
http://localhost:4000: the APIs and/health;http://localhost:4001: the tracking host (development tracking links point here);http://localhost:9568/metrics: Prometheus metrics.
curl -s http://localhost:4000/health
# {"database":"ok","status":"ok"}
To also start the SMTP listeners, generate a development CA once and start
with KARTA_DEV_SMTP=1:
mix karta.gen.dev_ca # writes priv/dev_ca (git-ignored)
KARTA_DEV_SMTP=1 iex -S mix phx.server
The listeners bind 127.0.0.1: 2525 (mx, refuses every recipient),
2587 (submission, STARTTLS) and 2465 (submissions, implicit TLS). The
certificates are valid for localhost, 127.0.0.1 and ::1; clients
verify them with priv/dev_ca/ca.pem.
Create an organisation, a server, a domain and keys
You can sign in to the admin pages at http://localhost:4000/login with
local login (on in development) after
mix run -e 'Karta.Release.local_user("dev@example.com", admin: true)',
which prints a password, and create all of this there. The same in the IEx
session:
{:ok, org} = Karta.Accounts.create_organisation(%{name: "Example"})
{:ok, server} = Karta.Servers.create_server(org, %{name: "Shop"})
{:ok, domain} = Karta.Domains.create_domain(org, %{name: "example.com"})
{:ok, domain} = Karta.Domains.verify_domain(domain)
{:ok, key} = Karta.Domains.generate_dkim_key(domain, %{algorithm: "ed25519"})
{:ok, _key} = Karta.Domains.activate_dkim_key(key)
{:ok, _credential, api_key} = Karta.Credentials.create_api_key(server, %{name: "Shop"})
{:ok, smtp, password} = Karta.Credentials.create_smtp_credential(server, %{name: "Shop"})
Karta.Credentials.username(smtp)api_key (karta_<prefix>_<secret>) and password are shown only here;
Karta keeps hashes. In production you would publish the DKIM key's record
first; see Domains and DNS.
Point delivery at Mailpit
Karta delivers to port 25 of the recipient's mail servers and never connects to loopback addresses. For this session, tell it to deliver to Mailpit's port and to allow the loopback address:
Application.put_env(:karta, Karta.Outbound,
Keyword.merge(Application.get_env(:karta, Karta.Outbound),
port: 51025, blocked_networks: []))Send to an address at localhost: the development resolver has no MX for
it, so Karta uses the implicit MX localhost (127.0.0.1), which is
Mailpit on the port set above. Do not use these settings outside
development.
Send a first message over HTTP
curl -s http://localhost:4000/api/v2/messages \
-H "Authorization: Bearer $KARTA_API_KEY" \
-H "Content-Type: application/json" \
-d '{"from": "Ann <ann@example.com>", "to": ["bob@localhost"],
"subject": "Hello from Karta", "text": "Hello Bob"}'
{
"id": "01a1bbdd-9013-75c5-93b8-65ef1e14249d",
"message_id": "<01a1bbdd-9013-75c5-93b8-65ef1e14249d@localhost>",
"recipients": [
{"address": "bob@localhost", "attempts": 0, "id": "01a1bbdd-9037-7e3d-811f-3c3fa383a897", "status": "queued"}
]
}The delivery job runs within a second. Read the message's state:
curl -s http://localhost:4000/api/v2/messages/01a1bbdd-9013-75c5-93b8-65ef1e14249d \
-H "Authorization: Bearer $KARTA_API_KEY"
The recipient's status is now delivered, with Mailpit's reply in
last_details (250 2.0.0 Ok: queued as ...). Open
http://localhost:58025: the message is there, with a DKIM-Signature
(a=ed25519-sha256; d=example.com) and Karta's Received: field
(by localhost with HTTP).
Send over SMTP
With the SMTP listeners started (KARTA_DEV_SMTP=1), any client that can do
STARTTLS and AUTH works. With Python:
import smtplib, ssl
ctx = ssl.create_default_context(cafile="priv/dev_ca/ca.pem")
with smtplib.SMTP("localhost", 2587) as s:
s.starttls(context=ctx)
s.login(USERNAME, PASSWORD) # Karta.Credentials.username/1 and the password
s.sendmail("ann@example.com", ["bob@localhost"],
b"From: Ann <ann@example.com>\r\nTo: bob@localhost\r\n"
b"Subject: Hello over SMTP\r\n\r\nHello Bob\r\n")Sending from a domain that is not verified for the organisation is refused
at MAIL FROM with 550 5.7.1 Sender address not permitted.
Try tracking
The development tracking host is http://localhost:4001. Turn tracking on
for the server and send an HTML message with a link:
{:ok, server} =
Karta.Servers.update_server(server, %{track_opens: true, track_clicks: true, list_unsubscribe: true})The copy in Mailpit has its links rewritten to http://localhost:4001/c/...,
an open pixel http://localhost:4001/o/....gif and a List-Unsubscribe
header. Loading them records events, which
GET /api/v2/messages/{id}/events lists. Because the development URL is
http://, Karta adds no List-Unsubscribe-Post field (RFC 8058 requires
https).
Start over
mix ecto.reset # drop and recreate karta_dev
docker compose -f dev/compose.yml down -v # remove the stack and its data
Next steps
- Sending rules and limits and the API pages describe what you can send and how.
- Development covers the test suite and the quality gate.