src/Controller/ContactController.php line 23

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. use Symfony\Component\Mime\Address;
  13. use Symfony\Component\Mime\Part\DataPart;
  14. use Symfony\Component\Mime\Part\File;
  15. use Symfony\Component\Mime\Part\FilePart;
  16. use Symfony\Component\Mime\Part\Multipart\MixedPart;
  17. class ContactController extends AbstractController
  18. {
  19.     #[Route('/contact'name'contact')]
  20.     public function contact(Request $requestMailerInterface $mailerGoogleRecaptchaService $recaptchaService): Response
  21.     {
  22.         $contact = new Contact();
  23.         $form $this->createForm(ContactType::class, $contact);
  24.         $form->handleRequest($request);
  25.         if ($form->isSubmitted() && $form->isValid()) {
  26. /*
  27.             $recaptchaResponse = $request->get('g-recaptcha-response');
  28.             $userIp = $request->getClientIp();
  29.             // Vérifier la validité de reCAPTCHA
  30.             if (!$recaptchaResponse || !$recaptchaService->verify($recaptchaResponse, $userIp)) {
  31.                 $this->addFlash('error', 'La validation reCAPTCHA a échoué. Veuillez réessayer.');
  32.                 return $this->redirectToRoute('contact');
  33.             }
  34. */
  35.             // Envoi de l'email
  36.             $email = (new Email())
  37.             ->from(new Address('cassaclock@gmail.com''Contact: Cassa Clock'))
  38.            //     ->from('papadembinho@gmail.com')
  39.                 ->to('cassaclock@gmail.com'// Remplacez par l'adresse de destination
  40.                 ->cc($contact->getEmail())
  41.                 ->replyTo($contact->getEmail())
  42.                 ->subject($contact->getSubject())
  43.                 ->text($contact->getMessage());
  44.                 $file $form->get('attachment')->getData();
  45.                 if ($file) {
  46.                     $email->attachFromPath(
  47.                         $file->getPathname(),
  48.                         $file->getClientOriginalName()
  49.                     );
  50.                 }
  51.             $mailer->send($email);
  52.             // Message de confirmation
  53.             $this->addFlash('success''Votre message a été envoyé avec succès.');
  54.             // Redirection après envoi
  55.             return $this->redirectToRoute('contact');
  56.         }
  57.         return $this->render('contact/contact.html.twig', [
  58.             'form' => $form->createView(),
  59.             'recaptcha_site_key' => $_ENV['RECAPTCHA_SITE_KEY']
  60.         ]);
  61.     }
  62. }