# Self-hosting

Run OpenSend on a server of your own: the dashboard, this API and the database in one program, with a sending engine underneath or Nextmail behind it.

## What you run

> Not released yet. The server and the engine are not published as packages, and the installer has only been tried on a development machine. Read this page as a preview of what is built.

One program, `opensend-server`, serves the dashboard, this API and these docs from one address. It keeps everything in one folder: a SQLite database, the files of your emails and what the engine stores. It also runs the schedule that sends broadcasts and moves automations along, once a minute.

| Way | What sends | What it costs |
| --- | --- | --- |
| Your own engine | The engine in `@nextmail/engine` starts inside the server. It signs with DKIM, delivers from your address and receives on port 25 | Nothing beyond the server |
| With Nextmail | Nextmail sends with a key of your account there, and your server keeps the data | What Nextmail charges for the mail you send |

The code is the same whoever hosts it, so every page of these docs holds on your server too. What differs is listed at the end of this page.

## What you need

- A server with Debian or Ubuntu and root access. The installer is written for those two.
- A name that points at it with an A record, such as `mail.example.com`. People open the dashboard there, and programs call the API there.
- Ports 80 and 443 open, for the certificate and for `https`.
- For your own engine: port 25 open in both directions, and a PTR record that names the server for its address. Many hosting companies keep port 25 closed on a new account until you ask them.

## Install it

The installer takes the address people will open and the email of the first account.

```bash
sudo bash install.sh https://mail.example.com you@example.com
```

1. It installs Caddy, which serves `https` and renews the certificate, and Bun, which runs the server.
2. It makes a user named `opensend` for the server to run as, and its folders.
3. It installs the server and the engine.
4. It writes the settings with a new secret, a service that starts with the machine, and the proxy entry.
5. It asks for the password of the first account, then starts everything.

Open the address and sign in with that email and password. On any other machine with Bun, the same can be done by hand: two settings are required, and the server then listens on `127.0.0.1:3000` behind a proxy of your choice.

```bash
bun add @open-send/opensend-server @nextmail/engine

export SERVER_URL=https://mail.example.com
export AUTH_SECRET="$(openssl rand -hex 32)"

bunx opensend-server owner you@example.com
bunx opensend-server start
```

## On a cloud server

A host that takes user data, such as Hetzner or DigitalOcean, can run the installer by itself when the server starts for the first time. `cloud-init.sh` is the same installer with your address on its third line, so it takes no arguments.

1. Change the address on line 3 of `cloud-init.sh`.
2. Create the server with that file as its user data, in the form of the host or with its tool.
3. Point the name at the server. The certificate arrives by itself once the name resolves.
4. Make the first account from a terminal. A first boot cannot ask for a password, so this one step is yours.

```bash
hcloud server create --name mail --type <type> --image <image> --ssh-key <key> --user-data-from-file cloud-init.sh
doctl compute droplet create mail --image <image> --size <size> --ssh-keys <key> --user-data-file cloud-init.sh
```

```bash
ssh -t root@<server> opensend-server owner you@example.com
```

What happened at the first boot is in `/var/log/cloud-init-output.log` on the server. The server needs an IPv4 address, because the runtime is downloaded from a host that has no IPv6.

## Run and upgrade it

```bash
opensend-server status
opensend-server logs
opensend-server restart
opensend-server upgrade
```

| Command | What it does |
| --- | --- |
| `status`, `logs` | Shows whether the service runs, or follows what it writes |
| `start`, `stop`, `restart` | Controls the service |
| `upgrade` | Installs the newest server and engine, then restarts |
| `owner <email>` | Makes the account that owns the server. For an account that exists, it hands the server to that account |
| `password <email>` | Gives an account a new password and signs it out everywhere |

Everything the server stores is under `SERVER_DATA_DIR`, which the installer sets to the `opensend` folder in `/var/lib`. Stop the service and copy that folder to back it up. The migrations of a new version run by themselves when it starts.

## Settings

The settings are environment variables. The installer keeps them in `server.env`, in the `opensend` folder of `/etc`, where only root reads them. Restart after a change.

| Name | Default | What it is |
| --- | --- | --- |
| `SERVER_URL` | Required | The address people open |
| `AUTH_SECRET` | Required | At least 32 characters, made once and never changed. It signs every session |
| `SERVER_HOST`, `SERVER_PORT` | `127.0.0.1`, `3000` | Where it listens, behind the proxy |
| `SERVER_DATA_DIR` | `data` | The folder for the database, the files and the engine |
| `SERVER_ENGINE` | `on` | `off` never starts the engine |
| `NEXTMAIL_API_KEY` | None | A key of your Nextmail account. With it the server sends through Nextmail and the engine stays off |
| `MAIL_FROM` | None | The sender of the server's own mail: sign-in codes and invitations |
| `SUPPORT_EMAIL` | None | Where an answer to the server's own mail goes, and where the contact form of the site sends a message |
| `TURNSTILE_SITE_KEY`, `TURNSTILE_SECRET_KEY` | None | The two keys of a Cloudflare Turnstile widget made for your address. With both set, asking for a sign-in code needs a passed check |
| `SERVER_SIGN_UPS` | `closed` | What a new server starts with: `open` or `closed` |
| `SERVER_PLAN_LIMITS` | None | The limits a new server starts with, as `name=number` pairs such as `members=10, domains=5` |

The last two only give a new server its first values. Once the owner changes them in the dashboard, the dashboard holds them, and the server says in its log at every start that the file no longer applies.

## Who gets in

The account made at install owns the server. It signs in with its email and password, because a new server cannot email a code yet.

- Sign-ups are closed, so people come in by invitation. Invite someone under Settings, Team.
- While the server sends no mail, copy the link beside the invitation and send it yourself. The person opens it, chooses a password and joins. A link works once and for seven days, sending the invitation again replaces it, and only the owner of the server is shown it.
- To let anybody make an account, switch on Open sign-ups under Settings, Server. A new person signs in with an emailed code, so the server has to send mail first.
- Everyone changes their own password on their profile page. For a forgotten one, run `opensend-server password <email>` on the server.

The server settings are read and changed through [the API](https://opensend.sh/docs/api/server) too. Only the owner changes them, signed in: a key belongs to one workspace, and these settings govern all of them.

## Limits

A workspace on your server is on the `self_hosted` plan. It has the limits you set and none where you set none. Set them under Settings, Server. A change applies at once to every workspace, the ones that exist already included.

| Limit | What it counts | At the limit |
| --- | --- | --- |
| `transactional` | Emails sent in a month through the API and automations | Sending is refused |
| `marketing` | Emails sent in a month as broadcasts | Broadcasts wait for the next month |
| `contacts` | Contacts held | No more can be added |
| `domains` | Domains added | No more can be added |
| `members` | People, waiting invitations included | No more can be invited |

Received emails and API requests are counted too, and nothing stops at a number for them.

## Send and receive

1. Add your domain in the dashboard and publish the DNS records it shows. [Domains](https://opensend.sh/docs/domains) explains each one.
2. Give the address of the server a PTR record that names it, and check that port 25 is open both ways.
3. Send a first email from the dashboard or the API, and watch it arrive.
4. Set `MAIL_FROM` to an address on that domain and restart. From then on the server emails sign-in codes and invitations by itself.

```bash
MAIL_FROM="Acme <hello@mail.example.com>"
```

To send through Nextmail instead, set `NEXTMAIL_API_KEY` and add the domain the same way. The records are then the ones Nextmail asks for.

## What is different on your server

- There are no plans to buy. Every workspace is on the `self_hosted` plan with the limits its server sets.
- The examples in these docs name our cloud. On your server the address is your own `SERVER_URL`: set `OPENSEND_API_URL` to it, or give the client `baseUrl`.
- An account made at install, or through an invitation link, signs in with a password. The hosted product has no passwords.
- Delivery is yours to earn: a new address has no reputation with the large mail providers, so start with small volumes.
