email.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. use AuditExpress\Engine;
  4. use AuditExpress\Http;
  5. use AuditExpress\LogMailer;
  6. use AuditExpress\PackRepository;
  7. use AuditExpress\PdfRenderer;
  8. use AuditExpress\RulesRepository;
  9. $root = require __DIR__ . '/_bootstrap.php';
  10. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  11. Http::methodNotAllowed(['POST']);
  12. exit;
  13. }
  14. $body = Http::readJsonBody();
  15. $packId = (string) ($body['pack_id'] ?? 'onboarding-v1');
  16. $answers = $body['answers'] ?? [];
  17. $optional = $body['optional'] ?? [];
  18. $email = (string) ($body['email'] ?? '');
  19. $bookingUrl = (string) ($body['booking_url'] ?? '');
  20. if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  21. Http::json(['error' => 'Email invalide'], 400);
  22. exit;
  23. }
  24. if (!is_array($answers) || !is_array($optional)) {
  25. Http::json(['error' => 'Invalid payload'], 400);
  26. exit;
  27. }
  28. try {
  29. $packRepo = new PackRepository($root);
  30. $rulesRepo = new RulesRepository($root);
  31. $pack = $packRepo->getPack($packId);
  32. $rules = $rulesRepo->getRules();
  33. $result = Engine::compute($pack, $answers, $optional, $rules);
  34. $pdfBytes = (new PdfRenderer($root))->renderPdfBytes($pack, $result, [
  35. 'booking_url' => $bookingUrl,
  36. 'date' => date('Y-m-d'),
  37. ]);
  38. $filename = 'controle-technique-' . ($pack['pack_id'] ?? 'resultat') . '.pdf';
  39. $subject = 'Votre contrôle technique : synthèse PDF';
  40. $text = "Bonjour,\n\nVoici votre synthèse PDF.\n\nCordialement,\n";
  41. $mailer = new LogMailer($root . '/storage/outbox');
  42. $mailer->sendPdf($email, $subject, $text, $filename, $pdfBytes);
  43. Http::json([
  44. 'status' => 'stored',
  45. 'message' => 'V1 pseudo: email stocké dans storage/outbox (à brancher sur SMTP/API).',
  46. ]);
  47. } catch (Throwable $e) {
  48. Http::json(['error' => $e->getMessage()], 400);
  49. }