# One-time codes by mail and SMS

One form of [two-factor sign-in](Security-Two-Factor.md): after the password, the server sends the user a short code, by mail or as a text message, and the user types it. Nothing has to be installed on the user's side.

It is an Enterprise feature.

## Mail or SMS?

| | By mail | By SMS |
|---|---|---|
| needs | a working mail server, and a mail address on each account | an SMS service with a web interface, such as Twilio, and a phone number on each account |
| costs | nothing | a fee per message |
| as strong as | the user's mailbox | the user's phone number |
| fails when | mail is slow or lands in the junk folder | there is no signal, or the number changed |

Both are weaker than an authenticator app or a passkey, because the code travels. They are much better than a password alone.

## The server's side

In **OTP Settings** of [General Settings](Prefs-General.md#otp-settings):

1. Turn on **Validated Logins?**. Without it nobody is asked for a code.
2. Say how codes are sent.

**By mail:** enter the four letters `SMTP` as the **URL**, and nothing else. The server then mails the code through its [mail server](Prefs-General-SMTP.md). The text of the mail is the [email template](Prefs-Email-Templates.md) named **Two Factor Auth**, with `{auth_token}` for the code; without that template the mail says "OTP password :" and the code.

**By SMS:** enter the address of the service as the **URL** and what is posted to it as **API Post**. For Twilio:

| Field | Enter |
|---|---|
| **URL** | `https://{otp_username}:{otp_password}@api.twilio.com/2010-04-01/Accounts/{otp_username}/Messages.json` |
| **API Post** | `To={otp_to}&From={otp_from}&Body={otp_token}` |
| **Username** | the Account SID |
| **Password** | the Auth Token |
| **From** | your Twilio phone number |

The variables are replaced for each message: `{otp_to}` is the user's phone number, `{otp_token}` the code. Any service that takes such a request works; the service must answer with a success status.

3. Press **Test OTP Settings** to send a test message: see [Testing settings](Prefs-General-Tests.md#test-otp-settings).

A server that reaches the internet through a proxy or a strict firewall must be let out to the service, such as `api.twilio.com`.

## The user's side

On the [Setup](Users-Setup.md#a-second-factor) tab of the user, or of a template:

| Setting | What to set |
|---|---|
| **Two factor OTP/SMS** | **All**, or the protocols on which a code is asked |
| **Phone** | the number, for SMS, in the form the service wants, with the country code |
| the mail address | for mail |
| **OTP auto valid for** | days during which the same device is not asked again |

A user without a phone number gets no SMS, and a user without a mail address no mail: the login then cannot be completed. Check the data before you turn it on for a group.

## What the user sees

After name and password, the web interface asks for the code and the server sends it. The user types it. A wrong or late code is refused like a wrong password, and counts as a failed login.

How long the code is, is the **Generated name length** of the shares' settings: see [Temp accounts](Temp-Accounts.md#general-settings).

## Things to know

- Behind a DMZ server, turn on **Validated Logins?** on the DMZ server too: it is the one that asks.
- Codes are for people at a screen. On which protocols a user is asked is set per user; leave out those whose programs cannot ask.
- Every code is a message that costs money or time. **OTP auto valid for** keeps people from being asked ten times a day.
- When messages do not arrive, look in the server log for the answer of the service: a failure is written there with the status the service gave.
