src/Controller/ContactController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactType;
  4. use App\Repository\LabelRepository;
  5. use Symfony\Component\Mime\Address;
  6. use App\Repository\SettingRepository;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. class ContactController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/contact", name="app_contact")
  17.      */
  18.     public function index(
  19.         SettingRepository $settingRepository,
  20.         LabelRepository $labelRepository,
  21.         Request $request,
  22.         MailerInterface $mailer
  23.     ): Response
  24.     {
  25.         $form $this->createForm(ContactType::class);
  26.         $form->handleRequest($request);
  27.         $dataMail $settingRepository->findAll();
  28.         $siteEmail $dataMail[0]->getSiteEmail();
  29.         $siteName $dataMail[0]->getSiteName();
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.             $contactFormData $form->getData();
  32.             $email = (new TemplatedEmail())
  33.                 ->from($contactFormData['email'])
  34.                 ->to(new Address($siteEmail$siteName))
  35.                 //->subject('Message depuis votre site web: ' . $contactFormData['objet'])
  36.                 ->subject('Message depuis votre site web')
  37.                 ->htmlTemplate('contact/contact_email.html.twig')
  38.                 ->context([
  39.                     'sender' => $contactFormData['prenom'] . ' ' $contactFormData['nom'],
  40.                     //'objet' => $contactFormData['objet'],
  41.                     'senderEmail' => $contactFormData['email'],
  42.                     'telephone' => $contactFormData['telephone'],
  43.                     //'rappel' => $contactFormData['rappel'],
  44.                     'message' => trim(nl2br($contactFormData['message'])),
  45.                     'settings' => $settingRepository->findAll(),
  46.                 ])
  47.             ;
  48.             $mailer->send($email);
  49.             $this->addFlash('success''Merci pour votre message. Nous vous recontacterons dans les plus brefs délais.');
  50.             return $this->redirectToRoute('app_contact');
  51.         }
  52.         return $this->render('contact/index.html.twig', [
  53.             'form' => $form->createView(),
  54.             'settings' => $settingRepository->findAll(),
  55.             'labels' => $labelRepository->findAll(),
  56.             'pageTitle' => 'contact'
  57.         ]);
  58.     }
  59. }