---
phase: 13-email-mailboxes
plan: 01
subsystem: settings
tags: [email, smtp, encryption, crud]

requires:
  - phase: none
    provides: n/a (first phase of v0.4)
provides:
  - email_mailboxes table with SMTP credential storage
  - email_templates table (schema only, populated in phase 14)
  - email_logs table (schema only, populated in phase 15)
  - EmailMailboxController + EmailMailboxRepository CRUD
  - SMTP connection test endpoint
affects: [14-email-templates, 15-email-sending]

tech-stack:
  added: []
  patterns: [SMTP socket test via stream_socket_client, password encryption reusing IntegrationSecretCipher]

key-files:
  created:
    - database/migrations/20260315_000054_create_email_mailboxes_table.sql
    - database/migrations/20260315_000055_create_email_templates_table.sql
    - database/migrations/20260315_000056_create_email_logs_table.sql
    - src/Modules/Settings/EmailMailboxController.php
    - src/Modules/Settings/EmailMailboxRepository.php
    - resources/views/settings/email-mailboxes.php
  modified:
    - routes/web.php
    - resources/views/layouts/app.php

key-decisions:
  - "SMTP test via native stream_socket_client — no PHPMailer dependency yet"
  - "Password encryption via existing IntegrationSecretCipher (AES-256-CBC + HMAC)"
  - "is_default flag with auto-reset of previous default on save"

patterns-established:
  - "Email mailbox CRUD pattern (same as ReceiptConfig)"
  - "AJAX JSON test endpoint for connection validation"

duration: ~15min
started: 2026-03-15T00:00:00Z
completed: 2026-03-15T00:15:00Z
---

# Phase 13 Plan 01: DB + Skrzynki pocztowe Summary

**3 migracje email (mailboxes/templates/logs) + pełny CRUD skrzynek pocztowych SMTP z testem połączenia i szyfrowaniem haseł**

## Performance

| Metric | Value |
|--------|-------|
| Duration | ~15min |
| Tasks | 3 completed (2 auto + 1 checkpoint) |
| Files created | 6 |
| Files modified | 2 |

## Acceptance Criteria Results

| Criterion | Status | Notes |
|-----------|--------|-------|
| AC-1: Migracje tworzą 3 tabele | Pass | email_mailboxes, email_templates, email_logs z FK i indeksami |
| AC-2: CRUD skrzynek pocztowych | Pass | Dodawanie, edycja (z zachowaniem hasła), usuwanie, toggle statusu |
| AC-3: Walidacja połączenia SMTP | Pass | AJAX POST z AUTH LOGIN, JSON response |
| AC-4: Nawigacja | Pass | Link "Skrzynki pocztowe" w sidebar Settings |

## Accomplishments

- 3 tabele bazodanowe: `email_mailboxes` (SMTP credentials), `email_templates` (szablony), `email_logs` (historia wysyłki)
- Pełny CRUD skrzynek z szyfrowaniem haseł przez IntegrationSecretCipher
- Test połączenia SMTP z pełnym handshake (EHLO → STARTTLS → AUTH LOGIN)
- Widok z listą, formularzem edycji i AJAX testem połączenia

## Files Created/Modified

| File | Change | Purpose |
|------|--------|---------|
| `database/migrations/20260315_000054_create_email_mailboxes_table.sql` | Created | Tabela skrzynek SMTP |
| `database/migrations/20260315_000055_create_email_templates_table.sql` | Created | Tabela szablonów email |
| `database/migrations/20260315_000056_create_email_logs_table.sql` | Created | Tabela logów wysyłki |
| `src/Modules/Settings/EmailMailboxRepository.php` | Created | Repository: listAll, findById, save, delete, toggleStatus, listActive |
| `src/Modules/Settings/EmailMailboxController.php` | Created | Controller: index, save, delete, toggleStatus, testConnection |
| `resources/views/settings/email-mailboxes.php` | Created | Widok: lista + formularz + AJAX test |
| `routes/web.php` | Modified | 5 nowych route'ów + import EmailMailbox* + IntegrationSecretCipher |
| `resources/views/layouts/app.php` | Modified | Link "Skrzynki pocztowe" w sidebar |

## Decisions Made

| Decision | Rationale | Impact |
|----------|-----------|--------|
| Natywny stream_socket_client do testu SMTP | Brak potrzeby dodawania PHPMailer w tej fazie — wystarczy weryfikacja połączenia | PHPMailer/SwiftMailer dodany w fazie 15 |
| IntegrationSecretCipher do haseł SMTP | Reuse istniejącego mechanizmu AES-256-CBC+HMAC | Spójny wzorzec szyfrowania w projekcie |
| Auto-reset is_default przy save | Tylko jedna skrzynka domyślna | Uproszczenie logiki wyboru skrzynki |

## Deviations from Plan

### Summary

| Type | Count | Impact |
|------|-------|--------|
| Auto-fixed | 0 | - |
| Scope additions | 1 | Minimal — listActive() w repository |
| Deferred | 0 | - |

**Total impact:** Minimalna zmiana — dodano `listActive()` do repo (potrzebne w fazie 14 do selecta skrzynek).

### Deferred Items

None.

## Issues Encountered

None.

## Next Phase Readiness

**Ready:**
- Tabela `email_templates` gotowa na CRUD (faza 14)
- `EmailMailboxRepository::listActive()` gotowe do użycia w select skrzynki w szablonach
- Tabela `email_logs` gotowa na wypełnianie (faza 15)

**Concerns:**
- sonar-scanner nie uruchomiony (required skill) — do uruchomienia przed kolejnym UNIFY

**Blockers:**
- None

---
*Phase: 13-email-mailboxes, Plan: 01*
*Completed: 2026-03-15*
