src/Controller/Front/ContactsController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Component\Mime\Email;
  11. use Symfony\Component\Mime\Address;
  12. use OlaSoft\Common;
  13. use App\Entity\Messages;
  14. use App\Form\MessagesType;
  15. class ContactsController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/contacts/", name="contacts")
  19.      */
  20.     public function contact(MailerInterface $mailer)
  21.     {
  22.         $request $this->get('request_stack')->getCurrentRequest();
  23.         $em $this->getDoctrine()->getManager();
  24.         $msg = new Messages();
  25.         $form $this->createForm(MessagesType::class, $msg);
  26.         $form->handleRequest($request);
  27.         $captchaResponse $_POST['g-recaptcha-response'] ?? null;
  28.         if ($form->isSubmitted() && $form->isValid() && $captchaResponse){
  29.             $captchaUrl Common::getParameter("recaptcha")["url"];
  30.             $captchaSecret Common::getParameter("recaptcha")["secret"];
  31.             $userIP $_SERVER['REMOTE_ADDR'];
  32.             $captcha file_get_contents($captchaUrl '?secret=' $captchaSecret '&response=' $captchaResponse'&remoteip=' $userIP);
  33.             $captcha json_decode($captcha);
  34.             if($captcha->success){
  35.                 $browser Common::getBrowser(nulltrue);
  36.                 $msg->setIP($request->getClientIP());
  37.                 $msg->setBrowser($browser['name']);
  38.                 $msg->setOS($browser['platform']);
  39.                 $msg->setDate(new \DateTime);
  40.                 $em->persist($msg);
  41.                 $em->flush();
  42.                 $author $msg->getFName().' '.$msg->getLName();
  43.                 $message"<b>Nom et prénoms : </b>".$author.
  44.                     "<br/><b>Adresse électronique : </b>".$msg->getEmail().
  45.                     "<br/><b>Object : </b>".$msg->getObject().
  46.                     "<br/><b>Message :</b><p style='border-left: 3px solid #ccc; padding-left: 10px; padding-top: 5px; padding-bottom: 5px;'>".nl2br($msg->getContent())."</p>";
  47.                 $email = (new TemplatedEmail())
  48.                     // ->from('noreply@'.$_SERVER['HTTP_HOST'])
  49.                     ->to(Common::getParameter('contacts')['email'])
  50.                     ->replyTo(new Address($msg->getEmail(),$author))
  51.                     ->priority(Email::PRIORITY_HIGH)
  52.                     ->subject(Common::getParameter('sitename').' - '.$msg->getObject())
  53.                     ->html($message);
  54.                 $mailer->send($email);
  55.                 $this->addFlash("notice","Votre message a été envoyé avec succès.");
  56.                 return $this->redirectToRoute('contacts');
  57.             }
  58.             $this->addFlash("error","Une erreur s'est produite, veuillez cocher le Captcha svp.");
  59.         }
  60.         return $this->render('Default/contacts.html.twig',['form'=>$form->createView()]);
  61.     }
  62. }