# Keycloak

Send Keycloak's password resets, email verifications and other mail through Sendora's SMTP.

Send the mail that Keycloak writes to its users, such as password resets and requests to verify an address, through Sendora's SMTP. Each realm has its own email settings, so enter them in every realm that sends mail.

## 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 Keycloak's mail comes from.
- An administrator's access to the realm in Keycloak's admin console.

## Set up Keycloak

1. Add the domain that Keycloak'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 Keycloak's admin console, choose the realm, then open **Realm settings** and its **Email** tab. Enter Sendora's values:

| Field             | Value                                                                    |
| ----------------- | ------------------------------------------------------------------------ |
| From              | An address on your verified domain, such as `no-reply@example.se`        |
| From display name | The name your users see, such as your app's                              |
| Envelope from     | Empty. Sendora sets the envelope sender itself, so that bounces reach it |
| Host              | `smtp.sendora.se`                                                        |
| Port              | `465`                                                                    |
| Encryption        | Tick **Enable SSL**, and leave **Enable StartTLS** unticked              |
| Authentication    | On, with **Authentication Type** set to **Password**                     |
| Username          | The server key                                                           |
| Password          | The server key                                                           |
| Allow UTF-8       | Off, since Sendora takes addresses in ASCII only                         |

Port 587 works too, with **Enable StartTLS** ticked instead of **Enable SSL**. If Keycloak runs on a host that blocks ports 465 and 587, such as a DigitalOcean Droplet, use port 2525 in the same way.

4. Click **Save**.
5. Click **Test connection**, which sends a message to the address of the administrator you are signed in as. If the button stays disabled, send a message to a user instead. On the user's **Credentials** tab, click **Credential Reset**, choose a reset action and click **Send Email**.
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 **Username** and **Password** in place of the test key, and click **Save**.

## If a message is refused

When Sendora refuses a message, Keycloak logs `Failed to send email` with Sendora's reply, and **Test connection** shows **Error! Failed to send email**. `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 Keycloak sends a message, click **Check now**.
