# Payload

Send Payload's password resets and email verifications through Sendora's SMTP.

Send the mail that Payload 3 writes through Sendora's SMTP, such as password resets and the emails that verify a new user's address. Payload sends it through an email adapter, which this guide sets up in your project's `payload.config.ts`.

## 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 Payload's mail comes from.
- Your Payload project's code, and a user who can sign in to its admin panel.

## Set up Payload

1. Add the domain that Payload'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 Payload's Nodemailer adapter. Payload's packages share one version, so use the version of `payload` in your `package.json` in place of `3.94.2`:

```bash
npm install @payloadcms/email-nodemailer@3.94.2
```

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

```bash
SENDORA_API_TOKEN=sk_test_…
```

5. In `payload.config.ts`, pass the adapter to `buildConfig` as `email`, and keep the settings the file has already:

```ts
import { nodemailerAdapter } from '@payloadcms/email-nodemailer';

export default buildConfig({
  // The settings the file has already
  email: nodemailerAdapter({
    defaultFromAddress: 'no-reply@example.se',
    defaultFromName: 'Example',
    transportOptions: {
      host: 'smtp.sendora.se',
      port: 465,
      secure: true,
      auth: {
        user: process.env.SENDORA_API_TOKEN,
        pass: process.env.SENDORA_API_TOKEN,
      },
    },
  }),
});
```

Put an address on your verified domain in `defaultFromAddress`. Payload sends its mail from it, with `defaultFromName` as the sender's name.

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

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

6. Restart Payload. `npm run start` runs Payload from its build, so run `npm run build` first.
7. Sign out of the admin panel, or open it in a private window. On its sign-in page, click **Forgot password?**, enter your address in **Email**, and click **Submit**.
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 Payload.

## If a message is refused

When Sendora refuses a key or a message, Payload logs an error with Sendora's reply, such as `Invalid login: 535 5.7.8` or `Message failed: 550 5.7.1`. The admin panel's **Forgot password?** page still shows **Email Sent**. Through the REST API, a password reset fails with `500` and `Something went wrong.`, and so does creating a user whose address Payload must verify.

The adapter also signs in to Sendora when Payload loads its config, to check the settings. If Sendora refuses the key then, Payload logs `Error verifying Nodemailer transport.` with the reply.

`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 Payload sends a message, click **Check now**.
