src/Controller/MainController.php line 20

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Form\Extension\Core\Type\FormType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\Form\FormFactoryInterface;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. class MainController extends AbstractController {
  16.     #[Route('/'name'index')]
  17.     public function index(Request $requestFormFactoryInterface $formFactoryMailerInterface $mailer) {
  18.         if ($request->isXmlHttpRequest()) {
  19.             if (null !== $request->get('form_contact')) {
  20.                 parse_str($request->get('form_contact'), $form_contact_data);
  21.                 //dump($form_contact_data);
  22.                 $mail = (new Email())
  23.                     ->from('lopezmick66@gmail.com')
  24.                     ->to('lopezmick@protonmail.com')
  25.                     ->subject($form_contact_data['form_contact']['objet'])
  26.                     ->html('Nom : ' $form_contact_data['form_contact']['nom'] . '<br/>Email : ' $form_contact_data['form_contact']['email'] . '<br/><br/>' $form_contact_data['form_contact']['message'])
  27.                 ;
  28.                 $mailer->send($mail);
  29.             }
  30.         }
  31.         $form_contact $formFactory->createNamedBuilder('form_contact'FormType::class)
  32.             ->add('nom'TextType::class, [
  33.                 'required' => true
  34.             ])
  35.             ->add('email'TextType::class, [
  36.                 'required' => true
  37.             ])
  38.             ->add('objet'TextType::class, [
  39.                 'required' => true
  40.             ])
  41.             ->add('message'TextAreaType::class, [
  42.                 'attr' => [
  43.                     'rows' => 3
  44.                 ],
  45.                 'required' => true
  46.             ])
  47.             ->add('valider'SubmitType::class,  array(
  48.                 'label' => 'Envoyer'
  49.             ))
  50.             ->getForm();
  51.         return $this->render('base.html.twig', [
  52.             'form_contact' => $form_contact->createView()
  53.         ]);
  54.     }
  55.     #[Route('/legal'name'legal')]
  56.     public function legal(Request $request) {
  57.         return $this->render('legal.html.twig');
  58.     }
  59. }
  60. ?>