# Bounce mailbox ingress

Nuhet's scheduled `FetchBounceStats` command scans `storage/mail/bounce` for complete files. Postfix currently delivers bounce DSNs to the `bounce` account's Maildir instead. The `bounce` account should receive these messages through a per-recipient `mailbox_command_maps` entry that invokes `scripts/postfix-bounce-ingest.php` on standard input. The script stages the complete message under `storage/mail/bounce/.incoming` and atomically publishes it in the scan directory.

## Production preflight

1. Confirm the deployed commit contains the ingest script and `BounceSpoolWriter`, and verify `php -l` on both.
2. Record `postconf -n mailbox_command_maps mailbox_transport_maps mailbox_transport recipient_delimiter home_mailbox` and the existing `bounce` alias/`.forward` state. On 2026-09-25 the three mailbox map/transport parameters were unset and `recipient_delimiter = +`; stop if that changes before activation.
3. Preserve the current `/etc/postfix/main.cf`, any existing command map, and ownership/mode of `/home/bounce` and `storage/mail/bounce` for rollback.
4. Remove world write from `/home/bounce` after confirming other local users do not need it. Postfix checks `.forward` before command maps; no other local account should be able to create or replace `/home/bounce/.forward`.
5. Keep `storage/mail/bounce` owned by `www-data`; give it group `bounce` and mode `2775`. This preserves the directory's existing read/traverse access for operational users while granting `bounce` group write access. Create `.incoming` owned by `bounce:www-data` with mode `2770`. Postfix runs the command with the `bounce` user's primary group, not its supplementary groups. The setgid staging directory gives published files group `www-data`; the writer sets file mode `0640`.
6. Confirm the deployed script and `vendor/autoload.php` are readable by `bounce`, and that `bounce` can write both the staging and spool directories. Verify a staged file inherits group `www-data` and is readable by the app after publication.

## Routing activation

After the code commit is deployed and the system-service change is approved, these are the intended commands on `post.nuhet.se`. Stop if any guard fails; do not continue with partial routing.

```bash
(
set -e
cd /var/www/html
test "$(hostname)" = post.nuhet.se
test "$(postconf -h mailbox_command_maps)" = ''
test "$(postconf -h mailbox_transport_maps)" = ''
test "$(postconf -h mailbox_transport)" = ''
test "$(postconf -h home_mailbox)" = 'Maildir/'
test ! -e /home/bounce/.forward
test ! -e /etc/postfix/nuhet_bounce_commands
test ! -e storage/mail/bounce/.incoming
test "$(stat -c '%U:%G:%a' /home/bounce)" = 'bounce:webbgroup:777'
test "$(stat -c '%U:%G:%a' storage/mail/bounce)" = 'www-data:www-data:775'
sudo -u bounce test -r vendor/autoload.php
sudo -u bounce test -r scripts/postfix-bounce-ingest.php
sudo cp -a /etc/postfix/main.cf "/etc/postfix/main.cf.nuhet-bounce-$(date +%Y%m%d-%H%M%S).bak"
sudo chmod 0755 /home/bounce
sudo chgrp bounce storage/mail/bounce
sudo chmod 2775 storage/mail/bounce
sudo install -d -o bounce -g www-data -m 2770 storage/mail/bounce/.incoming
sudo -u bounce test -w storage/mail/bounce
sudo -u bounce test -w storage/mail/bounce/.incoming
sudo -u www-data test -r storage/mail/bounce
sudo -u www-data test -x storage/mail/bounce
sudo stat -c '%A %U:%G %n' /home/bounce storage/mail/bounce storage/mail/bounce/.incoming
)
```

Before enabling routing, test the writer in a hidden spool that `FetchBounceStats` does not scan:

```bash
(
set -e
cd /var/www/html
smoke=storage/mail/bounce/.2956-ingress-smoke
test ! -e "$smoke"
sudo install -d -o www-data -g bounce -m 2775 "$smoke"
sudo install -d -o bounce -g www-data -m 2770 "$smoke/.incoming"
published=$(printf 'Nuhet bounce ingress smoke\n' | sudo -u bounce /usr/bin/php -r '
require "vendor/autoload.php";
echo (new \App\Services\Mail\BounceSpoolWriter())->store(STDIN, $argv[1]);
' "$smoke")
test "$(sudo stat -c '%U:%G:%a' "$published")" = 'bounce:www-data:640'
sudo -u www-data grep -Fxq 'Nuhet bounce ingress smoke' "$published"
sudo -u bounce rm -- "$published"
sudo rmdir -- "$smoke/.incoming" "$smoke"
)
```

Confirm the smoke test exited successfully and the hidden spool is gone. Then create the map and enable it:

```bash
(
set -e
printf '%s\n' 'bounce /usr/bin/php /var/www/html/scripts/postfix-bounce-ingest.php' | sudo tee /etc/postfix/nuhet_bounce_commands >/dev/null
sudo chown root:root /etc/postfix/nuhet_bounce_commands
sudo chmod 0644 /etc/postfix/nuhet_bounce_commands
sudo postmap hash:/etc/postfix/nuhet_bounce_commands
test "$(sudo postmap -q bounce hash:/etc/postfix/nuhet_bounce_commands)" = '/usr/bin/php /var/www/html/scripts/postfix-bounce-ingest.php'
sudo postconf -e 'mailbox_command_maps = hash:/etc/postfix/nuhet_bounce_commands'
if ! sudo postfix check || ! sudo systemctl reload postfix; then
    sudo postconf -e 'mailbox_command_maps ='
    sudo systemctl reload postfix
    echo 'Postfix activation failed; restored the empty command map.' >&2
    exit 1
fi
postconf -n mailbox_command_maps
)
```

Do not add a `.forward` file or change global `home_mailbox`.

## Verification and rollback

Send one controlled message to a fresh isolated invalid recipient from the Nuhet test account. Check the outgoing envelope sender, Postfix DSN queue, local command-delivery log, and new file or processed bounce event. Confirm the receiver is marked hard bounce, inactivated, and excluded from the company receiver count. Confirm no unexpected deferred mail and no quarantine entry for the test. Check another local account still delivers by its existing route.

If delivery fails, run `sudo postconf -e 'mailbox_command_maps ='` and `sudo systemctl reload postfix` to restore the previously empty routing setting. Keep the map and staged files for diagnosis. Failed ingest exits `75` so Postfix can retry; do not discard queued DSNs. Reconcile any delivered files and original Maildir messages before replay, because command delivery and retries may duplicate a message.

The existing Maildir backlog is a separate review. Enabling the new route does not move or reprocess older messages automatically. Company 19812's historical events remain unconfirmed and must not be bulk-marked without recipient-level evidence.

## Historical exact-recipient reconciliation

The live route does not move old messages from `/home/bounce/Maildir`. After deploying the archive scanner and reconciliation command, use this company-scoped, read-only preview. It streams recipient evidence between the `bounce` and `www-data` processes; addresses are not printed or stored in a temporary file.

```bash
(
  set -euo pipefail
  cd /var/www/html
  test "$(hostname)" = post.nuhet.se
  sudo -u bounce python3 scripts/archive-hard-bounce-evidence.py --company 19812 |
    sudo -u www-data php artisan receivers:reconcile-archived-hard-bounces --company=19812
)
```

Review the archive SHA-256, complete-scan result, eligible receiver count, and each skipped category. Only then run the same pipeline with `--apply --expected-digest=<dry-run SHA-256> --expected-eligible=<dry-run eligible_receivers>`. Any archive change, incomplete scan, changed eligible count, company mismatch, malformed evidence, or missing expected value stops before writes. Each applied receiver is locked and its company, current email, and edit time are checked again. The command marks receivers; it does not move or delete archived Maildir files or create duplicate bounce events. Compare the final marked and counted receiver totals with a fresh read-only query after applying.