# Mastodon

Send a Mastodon server's sign-up confirmations, password resets and notifications through Sendora's SMTP.

Send the mail that your Mastodon server writes, such as sign-up confirmations, password resets, notifications and reports to moderators, through Sendora's SMTP. This guide is for a Mastodon that you run yourself, installed from source or with Docker. Both kinds read their settings from `.env.production` in Mastodon's folder.

## Before you start

- A Sendora account. A test server works from the moment the account exists. A live server sends once your account is approved. The account starts on Free, within the [limits](https://sendora.se/docs/limits#the-free-tier) that [Account › Usage](https://app.sendora.se/account/usage) in the dashboard shows.
- Access to the DNS of the domain that Mastodon's mail comes from.
- Root access to the machine that runs Mastodon, and an account on your Mastodon server.

## Set up Mastodon

1. Add the domain that Mastodon's mail comes from under [Domains](https://app.sendora.se/domains), and add the two DNS records it shows, as [Domains](https://sendora.se/docs/domains) explains.
2. Create a test server under [Servers › New server](https://app.sendora.se/servers/new), with **Testserver** ticked, and create a key on its **API keys** tab. The key starts with `sk_test_`. A [test server](https://sendora.se/docs/test-servers) takes mail as a live server does and delivers none of it.
3. In `.env.production` in Mastodon's folder, such as `/home/mastodon/live`, set these lines, with the server key as both `SMTP_LOGIN` and `SMTP_PASSWORD`. Remove the file's other `SMTP_` lines, such as those that Mastodon's setup wizard wrote.

```bash
SMTP_SERVER=smtp.sendora.se
SMTP_PORT=465
SMTP_TLS=true
SMTP_LOGIN=sk_test_…
SMTP_PASSWORD=sk_test_…
SMTP_OPENSSL_VERIFY_MODE=peer
SMTP_FROM_ADDRESS='Mastodon <notifications@example.se>'
```

Put an address on your verified domain in `SMTP_FROM_ADDRESS`. Mastodon sends all its mail from it.

With `SMTP_OPENSSL_VERIFY_MODE` set to `peer`, Mastodon checks Sendora's certificate and its name. The setup wizard writes `none` there if you press Enter when it asks for the verify mode, as [line 405 of its setup task](https://github.com/mastodon/mastodon/blob/v4.7.6/lib/tasks/mastodon.rake#L405) shows in 4.7.6. With `none`, Mastodon accepts any certificate, so a machine between Mastodon and Sendora could pose as Sendora and read the key.

Port 465 encrypts the connection from the first byte, and `SMTP_TLS` tells Mastodon to do so. Leave out `SMTP_ENABLE_STARTTLS` on 465, since Mastodon fails to send when it is `auto` or `always` beside `SMTP_TLS`.

Port 587 works too, with `SMTP_ENABLE_STARTTLS=always` in place of `SMTP_TLS=true`. Mastodon then fails rather than log in without TLS. Without that line, or with the wizard's `auto`, Mastodon starts TLS only if the server offers it, as [line 20 of its email settings code](https://github.com/mastodon/mastodon/blob/v4.7.6/lib/mastodon/email_configuration_helper.rb#L20) shows. If someone between Mastodon and Sendora removed that offer, Mastodon would then send the key unencrypted.

If Mastodon runs on a host that blocks ports 465 and 587, such as a DigitalOcean Droplet, use port 2525 as you would 587.

4. Restart Mastodon's web and Sidekiq processes, which read the file only when they start. In an install from source, run `sudo systemctl restart mastodon-web mastodon-sidekiq`. With Docker, run `docker compose up -d` in Mastodon's folder, which recreates the containers.
5. Sign out of Mastodon, or open it in a private window. On its sign-in page, click **Forgot your password?**, enter your address in **E-mail address**, and click **Reset password**.
6. Find the message on the server's **Messages** tab under [Servers](https://app.sendora.se/servers).
7. Once your account is approved, create a key on your live server's **API keys** tab, such as the default server that signup made. Put it in `SMTP_LOGIN` and `SMTP_PASSWORD` in place of the test key, and restart Mastodon's processes again.

## If a message is refused

Mastodon sends its mail in the background, through Sidekiq, so its pages show no error when Sendora refuses a message. Sidekiq's log holds the error with Sendora's reply, such as `Net::SMTPAuthenticationError` with `535 5.7.8`. In an install from source, `journalctl -u mastodon-sidekiq` shows the log, and with Docker, `docker compose logs sidekiq`.

Sidekiq tries a refused message again later. To see each one with Sendora's reply, open **Preferences** in Mastodon, then **Sidekiq** in its menu, and the **Retries** tab. The menu shows **Sidekiq** to users whose role has the **DevOps** permission, such as the Owner.

`535 5.7.8` means the key is wrong or revoked. `550 5.7.1` means the From domain is not verified, or that a live server's account is not yet approved. `451 4.7.0` means that a limit is reached, such as Free's [daily limit](https://sendora.se/docs/limits#the-free-tier). `550 5.1.1` means the recipient's address is on the [suppression list](https://sendora.se/docs/suppressions), for example after a hard bounce. [SMTP replies](https://sendora.se/docs/smtp#smtp-replies) lists every reply.

## Test the setup

On the server's **API keys** tab under [Servers](https://app.sendora.se/servers), **Test the setup** shows each valid key's latest sign-in and accepted message over SMTP. It also shows the key's latest refusals, each with what to do. After Mastodon sends a message, click **Check now**.
