
# Get your e-mails delivered

You set the SMTP relay and the sending domain so that access codes, schedules and chasers reach the inbox, then you learn to read what the application keeps of each send.

## 1. Connect the SMTP relay

In `.env.prod`:

```properties
MAIL_HOST=smtp.example.org
MAIL_PORT=587
MAIL_USERNAME=...
MAIL_PASSWORD=...
MAIL_START_TLS=REQUIRED
MAIL_FROM=planning@<domain>
```

`MAIL_HOST` and `MAIL_FROM` are mandatory. The production compose file defaults to `587` and `REQUIRED`, where the application alone goes to a local Mailpit (`1025`, no TLS). Also set `MAIL_ADMIN`: it is the address that receives swap requests, the end of a solve if the edition asks for it, the weather alert and the failure of the nightly backup.

Mails go out in the name of "<event> – organisation" (`MAIL_FROM_NAME` to choose another). For "Reply" to reach a person rather than the technical mailbox, set `MAIL_REPLY_TO`. The foot of the mails written to staff members names the event and, if the edition has a contact (Settings, the "Organisation contact" card), says whom to tell when the address is no longer the right one.

## 2. Set up the domain

On the domain side, the application can do nothing, and yet it decides whether the message is accepted: SPF (authorise the relay used), DKIM (sign, and publish the key) and DMARC (at least a monitoring policy, aligned with the domain of `MAIL_FROM`).

## 3. Test before the first send

Send a test schedule to **three different mailboxes**, including Gmail and Outlook, the strictest, and check that it reaches the inbox, not just that it leaves. The *Debug* page (Ctrl+K, or `/debug`, *Checks* tab) offers "Send a test mail".

Every mail goes out in **two parts**: plain text and an HTML version in the deployment's colours (`pdf-*` keys of `BRANDING_COLORS`, `BRANDING_ORGANISATION` in the footer), identical word for word. The logo (`pdf-logo` key of `BRANDING_IMAGES`) is embedded in the message: no image is loaded from a server on opening, which mail clients block and spam filters penalise. With `MAIL_MOCK=true`, nothing leaves: mails are logged, and the screen says "simulated" so an acceptance run is not mistaken for a broadcast.

## 4. Read what is kept of each send

Every e-mail addressed to a staff member (published or resent schedule, access code, invitation to declare, chasers, day-before reminder, swaps, carpooling, absences) leaves **one row** in the `envoi_mail` table: the person's identifier, the e-mail template, "sent" or "failed", the failure category and the date. Neither the address, nor the content, nor the mail server's answer, which readily quotes the refused address. This is what the *Acknowledgement* column of the Staff page ("send failure" instead of "silent"), the "Send failure" filter (`?envoi=echec`) and the staff fiche's signal read.

| Category | What the relay answered | What you do |
| --- | --- | --- |
| Server unreachable | nothing: connection refused, unknown name, timeout, TLS failure | check `MAIL_HOST`, `MAIL_PORT`, the network |
| Authentication | 530, 535, 5.7.8, and a 5.7.x that speaks of authentication ("relaying denied, authentication required") | check `MAIL_USERNAME` and `MAIL_PASSWORD` |
| Address refused | 5.1.x; 550, 551, 553 when no extended code says otherwise | correct the address on the fiche |
| Temporary refusal | any 4xx, and a full mailbox (552, 5.2.2) | nothing: the next send is allowed |
| Other | the rest: sender or relay refused (550 or 553 5.7.1), spam rejection, error before sending… | read the server log, check `MAIL_FROM` |

A 550 or 553 does not always mean "address refused": with an extended code, only a `5.1.x` targets the recipient, and a `553 5.7.1 Sender address rejected` or a `550 5.7.1 Relaying denied` targets the application (a `MAIL_FROM` the relay does not accept, a relay that does not allow sending).

**A refused address is no longer chased**, neither by hand nor at night, until the fiche has been edited since the failure: the same e-mail would meet the same refusal. Any edit of the fiche lifts the ban and the signal (no fingerprint of the address is kept); the next run of the automatic sends then chases the person, and "Remind now" can do it at once.

### What "sent" means

"Sent" means "handed to the relay", not "received" or "read". An authenticated relay almost always accepts the recipient and sends the refusal **later**, as a non-delivery notice addressed to `MAIL_FROM` that nobody picks up today: the "address refused" category will rarely be seen, the screen mostly shows relay outages.

### How long the rows stay

The rows live as long as the action history (`JOURNAL_RETENTION`, ninety days by default, see [data retention](conservation-des-donnees.md)): the night purge removes the oldest, all editions together, and they disappear with their edition. They travel in the SQL export and are not copied when an edition is duplicated. Day-before reminders and chasers go out from a scheduled job with its own rules: [the automatic sends](envois-automatiques.md).

## 5. Limiting the sending rate

Some relays cap sends per hour: a publication of a hundred and fifty mails can go over the cap, and the end of the list fails. `MAIL_MAX_PER_MINUTE` sets an instance limit, in mails per minute; `0`, the default, sets none and nothing changes.

Pick the value by dividing your relay's cap by sixty, with some margin: 500 mails an hour give at most 8 a minute, 6 to be safe. The mails then leave steadily, one every `60 / value` seconds: a hundred and fifty mails at 6 a minute take twenty-five minutes.

With a limit, **the publication alone** goes out in the background: it publishes the plan at once and answers "sending started", the *Send* tab of Share shows the progress ("63 / 150") and you can leave the screen. While it sends, **every other publication is refused**, "Notify the N people" of Today and of Swaps included: a hundred and fifty people at 10 a minute is a quarter of an hour of waiting, and that is what should decide your limit. A scheduled send ("Notify later") is not touched: it leaves at its hour for the people still to be told. After a restart, the progress counts only the people left ("12 / 87", not "63 / 150"). If the application restarts on the way, it resumes at boot where it was, without writing to the same person twice; a send that stops on an error is given back: it stays to be sent, and "Send now" or the hourly pass takes it up again. A planned publication follows the same rate. A negative value, like a value that is not a number, stops the application from starting, with a sentence saying so.

In this version the limit does not apply to the chaser, the team message, the individual send or the invitation to declare: they stay synchronous. The access code to the espace, the test mail and the account mails are never slowed. Only the mails of a publication are counted: if those other sends leave right after a large publication, keep your relay's cap in mind.
