# Email

Set up outgoing email: the verification, password reset and one-time code emails PocketBase sends for [auth](/docs/enable/auth), and anything else the app sends through it.

PocketBase reads its SMTP settings from the environment when it starts. This command fills them in for a provider, asking only for the key and the sender, and writes them to `.env` or to a deployed target. It generates no code.

### Syntax

```
$ vela enable email
```

```
$ vela enable email --provider resend --from hello@example.com   # into .env
$ vela enable email -t production                                # into a target, and restarts it
```

At a terminal the provider, the sender address and the key are asked for when they are not passed. The key is typed without echo and never printed back: the summary shows it as `••••`.

### Providers

- `resend` - `smtp.resend.com`, port 465. Asks for the API key
- `postmark` - `smtp.postmarkapp.com`, port 587. Asks for the server API token, which is both the username and the password
- `sendgrid` - `smtp.sendgrid.net`, port 465. Asks for the API key
- `mailgun` - `smtp.mailgun.org`, or `smtp.eu.mailgun.org` with `--region eu`, port 465. Asks for the domain's SMTP login and password, which are not the account's API key
- `ses` - `email-smtp.<region>.amazonaws.com`, port 465. Needs `--region`, and asks for the SES SMTP username and password, which are their own pair and not an IAM access key
- `smtp` - Any other SMTP server. Asks for the host, the port (587 unless you say otherwise), a username and a password

A provider's API key _is_ its SMTP password, so it is stored once, as `PB_SMTP_PASSWORD`. App code that calls the provider's own API can read it from there; there is no need for a second `RESEND_API_KEY`.

`--host`, `--port` and `--tls` / `--no-tls` win over the provider's row, so a provider that moves a port can be followed without waiting for a release.

### What gets written

```ts
PB_SMTP_HOST=smtp.resend.com    # setting it turns SMTP on
PB_SMTP_PORT=465
PB_SMTP_TLS=true                # TLS from the start; false upgrades with STARTTLS
PB_SMTP_USERNAME=resend
PB_SMTP_PASSWORD=••••
PB_SENDER_ADDRESS=hello@example.com
PB_SENDER_NAME=My App           # only with --from-name
```

These are the names listed under [PocketBase reads the same environment](/docs/env), and nothing else is touched: no migration, no setting saved in the database. In the [admin panel](/docs/admin) the mail settings show as locked, because they come from the environment.

- **Locally** (`-t local`, the default) the values go into `.env`, replacing a line that is already there. When the project has a `.env.example`, the seven names are added to it, empty, so a new checkout knows what to fill in. Restart [`vela dev`](/docs/dev) to pick them up.
- **On a target** (`-t production`, a named target, `-t preview`, `-t all`) they go into that target's [environment](/docs/env) on the server, the same way `vela env import` writes them: the password is secret and never read back, the rest shows in `vela env list`. The instances the layer reaches are restarted, which is when PocketBase reads the settings.

Values in `.env` are not deployed. Run the command once per target, or once with `-t all` for every target of the app.

To check the result, open `/admin`, then Settings, Mail settings, and send a test email. The sender address has to be one the provider accepts: a verified domain or address.

### Options

- `--provider <id>` - `resend`, `postmark`, `sendgrid`, `mailgun`, `ses` or `smtp`. Asked for at a terminal; `smtp` otherwise
- `--from <address>` - Address email is sent from (`PB_SENDER_ADDRESS`)
- `--from-name <name>` - Name email is sent as (`PB_SENDER_NAME`)
- `--api-key <key>` - The provider's API key or token, which is its SMTP password
- `--password <password>` - SMTP password; the same thing as `--api-key`
- `--password-stdin` - Read the API key or password from standard input
- `--username <username>` - SMTP username, for providers that do not fix one
- `--region <region>` - Required for `ses` (such as `us-east-1`); `us` or `eu` for `mailgun`
- `--host <host>` - SMTP host. Required for `smtp`, overrides the provider's otherwise
- `--port <port>` - SMTP port, overrides the provider's
- `--tls` / `--no-tls` - Connect with TLS from the start (port 465), or connect in the clear and upgrade with STARTTLS (port 587). Overrides the provider's
- `-t, --target <target>` - Which environment: `local`, `all`, `preview`, `preview:<branch>`, `production`, or any bound target such as `staging`
- `--server <ssh>` - Server this target runs on, recorded on first use
- `-i, --identity <file>` - SSH private key to authenticate with
- `--ssh-port <port>` - SSH port
- `--accept-host-keys` - Trust an unknown host key on first connect (CI)

### In CI

Off a terminal nothing is asked. Whatever is still missing is reported in one go, by flag name. Pass the key on standard input so it lands in neither the process list nor the shell history:

```
$ printf '%s' "$RESEND_API_KEY" | vela enable email --provider resend --from hello@example.com --password-stdin -t production
```

### Changing it later

Run the command again to switch provider; a username or password the new configuration does not use is removed rather than left behind. Change one value with [`vela env set`](/docs/env/set), for example `vela env set PB_SENDER_NAME -t production --public`. [`vela disable email`](/docs/disable/email) removes the settings.