# Strapi

Send Strapi's password resets, email confirmations and test email through Sendora's SMTP.

Send the mail that Strapi 5 writes through Sendora's SMTP, such as password resets and email confirmations. Strapi sends it with its Email plugin, through a provider that this guide adds to your project.

## 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 Strapi's mail comes from.
- Your Strapi project's code, and an administrator's account in its admin panel.

## Set up Strapi

1. Add the domain that Strapi'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 your project's folder, install the Nodemailer provider:

```bash
npm install @strapi/provider-email-nodemailer
```

4. Add the key to the project's `.env` file:

```bash
SENDORA_API_TOKEN=sk_test_…
```

5. In `config/plugins.ts`, add this `email` entry to the object that the file's function returns, and keep the entries it has already, such as `upload`. The function needs its `{ env }` argument to read the key.

```ts
import type { Core } from '@strapi/strapi';

const config = ({ env }: Core.Config.Shared.ConfigParams): Core.Config.Plugin => ({
  // The entries the file has already
  email: {
    config: {
      provider: 'nodemailer',
      providerOptions: {
        host: 'smtp.sendora.se',
        port: 465,
        secure: true,
        auth: {
          user: env('SENDORA_API_TOKEN'),
          pass: env('SENDORA_API_TOKEN'),
        },
      },
      settings: {
        defaultFrom: 'no-reply@example.se',
      },
    },
  },
});

export default config;
```

In a JavaScript project, the file is `config/plugins.js`, and its function begins `module.exports = ({ env }) => ({`.

Put an address on your verified domain in `defaultFrom`. Strapi sends from it when a message names no sender of its own, as the test email and the admin panel's password resets do.

Port 465 encrypts the connection from the first byte. Port 587 works too, with `secure` set to `false` and `requireTLS: true` beside it. Strapi sends through [Nodemailer](https://nodemailer.com/smtp#tls-options), which without `requireTLS` starts TLS only if the server offers it. Someone between Strapi and Sendora who removed that offer would then get the key unencrypted. With `requireTLS`, Strapi fails instead.

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

6. Restart Strapi. `npm run start` runs a TypeScript project from its build, so run `npm run build` first.
7. In the admin panel, open **Settings**, then **Configuration** under **Email plugin**. Under **Test email delivery**, enter your address in **Recipient email**, and click **Send test email**.
8. Find the message on the server's **Messages** tab under [Servers](https://app.sendora.se/servers).
9. 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 `SENDORA_API_TOKEN` in place of the test key, and restart Strapi.

The **Configuration** page shows the user name under **Authentication**, so every administrator who can open the page can read the key there. When someone loses that access, create a new key and revoke the old one.

## Users & Permissions mail

The Users & Permissions plugin sends your app's users their password resets and email confirmations from templates of its own. Each template sends from `no-reply@strapi.io` until you change it. Sendora refuses that address with `550 5.7.1`, since its domain is not verified on your server.

1. In the admin panel, open **Settings**, then **Email templates** under **Users & Permissions plugin**.
2. Click **Reset password**, put an address on your verified domain in **Shipper email**, and click **Finish**.
3. Do the same for **Email address confirmation**.

## If a message is refused

When Sendora refuses a key or a message, Strapi logs an error with Sendora's reply, such as `Invalid login: 535 5.7.8` or `Message failed: 550 5.7.1`. In the admin panel, the test email then shows **Failed to send a test mail to** your address, and an admin's password reset shows no error. Through the REST API, `/api/auth/forgot-password` fails with `500 Internal Server Error`, and a sign-up that sends a confirmation fails with `400` and `Error sending confirmation 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 Strapi sends a message, click **Check now**.
