src/Controller/Module/FormTerminController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Module;
  3. use Pimcore\Controller\FrontendController;
  4. use Symfony\Bridge\Twig\Attribute\Template;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Mime\Email;
  9. use Twig\Environment;
  10. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  11. class FormTerminController extends FrontendController
  12. {
  13.     /**
  14.      * @Route("/send-termin-form", name="handle_form_submission", methods={"POST"})
  15.      */
  16.     public function handleForm(Request $requestMailerInterface $mailerEnvironment $twig)
  17.     {
  18.         $subject $request->request->get('subject');
  19.         $name $request->request->get('name');
  20.         $email $request->request->get('email');
  21.         $service $request->request->get('service');
  22.         $location $request->request->get('location');
  23.         $date $request->request->get('date');
  24.         $time $request->request->get('time');
  25.         $url $request->headers->get('referer');
  26.         $htmlContent $twig->render('mail-template/form-termin.html.twig',
  27.             [
  28.                 "name" => $name,
  29.                 "email" => $email,
  30.                 "service" => $service,
  31.                 "location" => $location,
  32.                 "date" => $date,
  33.                 "time" => $time,
  34.                 "url" => $url,
  35.             ]);
  36.         // Create the email
  37.         $emailMessage = (new Email())
  38.             ->from('website@glanzer.at')
  39.             ->to('info@glanzer.at')
  40.             ->priority(Email::PRIORITY_HIGH)
  41.             ->subject($subject)
  42.             ->html($htmlContent);
  43.         try {
  44.             // Attempt to send the email
  45.             $mailer->send($emailMessage);
  46.             return $this->json([
  47.                 'success' => true,
  48.                 'message' => 'Termin successfully booked. Email sent successfully.',
  49.             ]);
  50.         } catch (TransportExceptionInterface $e) {
  51.             // Handle email sending failure
  52.             return $this->json([
  53.                 'success' => false,
  54.                 'message' => 'Termin booking failed. Could not send email.',
  55.                 'error' => $e->getMessage(), // Optional: Include the error message for debugging
  56.             ]);
  57.         }
  58.     }
  59.     public function formTerminAction(Request $request)
  60.     {
  61.         return $this->render("module/FormTermin/__form-termin.html.twig");
  62.     }
  63. }