src/Controller/ContactController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\Messenger\MessageBusInterface;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. // use App\Message\LowPriorityEmail;
  11. use App\Message\HighPriorityEmail;
  12. use App\Entity\Page;
  13. use App\Entity\Contact;
  14. use App\Form\ContactType;
  15. class ContactController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/contact", name="contact")
  19.      */
  20.     public function contact(Request $requestMessageBusInterface $bus)
  21.     {
  22.         $slug "contact";
  23.         $page $this->getDoctrine()->getRepository(Page::class)->findOneBySlug($slug);
  24.         if (!$page) {
  25.             throw $this->createNotFoundException('Pas de page avec le slug "' $slug '".');
  26.         }
  27.         
  28.         $contact = new Contact();
  29.         $form $this->createForm(ContactType::class, $contact);
  30.         $form->handleRequest($request);
  31.         if ($form->isSubmitted() && $form->isValid()) {
  32.             $contact $form->getData();
  33.             $entityManager $this->getDoctrine()->getManager();
  34.             $entityManager->persist($contact);
  35.             $entityManager->flush();
  36.             // dump($contact->getMessage());
  37.             // exit;
  38.             
  39.             $copyToSender true;
  40.             $isSent $this->_sendContactMail($contact$entityManager$bus$copyToSender);
  41.             $this->addFlash(
  42.                 'notice-popup',
  43.                 'Votre mail a été envoyé. Nous vous répondrons sous peu. Merci.'
  44.             );
  45.             return $this->redirectToRoute('home');
  46.         }
  47.         return $this->render('contact/index.html.twig', [
  48.             'page' => $page,
  49.             'form' => $form->createView(),
  50.         ]);
  51.     }
  52.     private function _sendContactMail(Contact $contact$entityManager$bus$copyToSender true) : bool
  53.     {
  54.         // MAIL POUR LE CONTACT DU SITE
  55.         // -> avec méthode messenger
  56.         $fromEmail $_ENV['FROM_MAIL'];
  57.         $toEmails "ici-Grenoble <" $_ENV['CONTACT_MAIL'] . ">";
  58.         $subject $contact->getSubject();
  59.         $content "<p><strong>Contact depuis le site ici-grenoble par : " $contact->getName() . " (" $contact->getEmail() . ")</strong></p>\n";
  60.         $content .= $contact->getMessage();
  61.         $contentHtml null;
  62.         $ccEmails =  null;
  63.         $bccEmails =  null;
  64.         $replytoEmail $contact->getName() . " <" $contact->getEmail() . ">";
  65.         $beforeSubject null// = ""; // defaults to App\Message\BaseEmail::DEFAULT_BEFORE_SUBJECT
  66.         // will cause the HighPriorityEmailHandler to be called
  67.         $bus->dispatch(new HighPriorityEmail($fromEmail$toEmails$subject$content$contentHtml$ccEmails$bccEmails$replytoEmail$beforeSubject));
  68.         // COPIE POUR L'ENVOYEUR
  69.         if($copyToSender) {
  70.             // -> avec méthode messenger
  71.             $fromEmail $_ENV['FROM_MAIL'];
  72.             $toEmails $contact->getName() . " <" $contact->getEmail() . ">";
  73.             $subject $contact->getSubject();
  74.             $content "<p><strong>Contact depuis le site ici-grenoble par : " $contact->getName() . " (" $contact->getEmail() . ")<br>-> copie à l'envoyeur</strong></p>\n";
  75.             $content .= $contact->getMessage();
  76.             $contentHtml null;
  77.             $ccEmails =  null;
  78.             $bccEmails =  null;
  79.             $replytoEmail $_ENV['REPLYTO_MAIL'];  
  80.             $beforeSubject null// = ""; // defaults to App\Message\BaseEmail::DEFAULT_BEFORE_SUBJECT
  81.             // will cause the HighPriorityEmailHandler to be called
  82.             $bus->dispatch(new HighPriorityEmail($fromEmail$toEmails$subject$content$contentHtml$ccEmails$bccEmails$replytoEmail$beforeSubject));
  83.         }
  84.         $contact->setSent(true); // je considère que le mail est envoyé, même s'il y a erreur... 
  85.         $entityManager->persist($contact);
  86.         $entityManager->flush();
  87.         $isSent true;
  88.         return $isSent;
  89.     }
  90. }