src/Controller/ContactController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactType;
  4. use App\Entity\Contact;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Email;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use App\Service\GoogleRecaptchaService
  12. class ContactController extends AbstractController
  13. {
  14.     #[Route('/contact'name'contact')]
  15.     public function contact(Request $requestMailerInterface $mailerGoogleRecaptchaService $recaptchaService): Response
  16.     {
  17.         $contact = new Contact();
  18.         $form $this->createForm(ContactType::class, $contact);
  19.         $form->handleRequest($request);
  20.         if ($form->isSubmitted() && $form->isValid()) {
  21.             $recaptchaResponse $request->get('g-recaptcha-response');
  22.             $userIp $request->getClientIp();
  23.             // Vérifier la validité de reCAPTCHA
  24.             if (!$recaptchaResponse || !$recaptchaService->verify($recaptchaResponse$userIp)) {
  25.                 $this->addFlash('error''La validation reCAPTCHA a échoué. Veuillez réessayer.');
  26.                 return $this->redirectToRoute('contact');
  27.             }
  28.             // Envoi de l'email
  29.             $email = (new Email())
  30.                 ->from('commandes.yobaale@gmail.com')
  31.                 ->to('commandes.yobaale@gmail.com'// Remplacez par l'adresse de destination
  32.                 ->cc($contact->getEmail())
  33.                 ->replyTo($contact->getEmail())
  34.                 ->subject($contact->getSubject())
  35.                 ->text($contact->getMessage());
  36.             $mailer->send($email);
  37.             // Message de confirmation
  38.             $this->addFlash('success''Votre message a été envoyé avec succès.');
  39.             // Redirection après envoi
  40.             return $this->redirectToRoute('contact');
  41.         }
  42.         return $this->render('contact/contact.html.twig', [
  43.             'form' => $form->createView(),
  44.         ]);
  45.     }
  46. }