src/Controller/Front/ContactController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Form\ContactType;
  4. use Symfony\Component\Mime\Email;
  5. use Symfony\Component\Mime\Address;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  13. /**
  14.  * @Route("/contact", name="app_main_")
  15.  */
  16. class ContactController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="contact", methods={"GET", "POST"})
  20.      */
  21.     public function index(Request $requestMailerInterface $mailer): Response
  22.     {
  23.         $form $this->createForm(ContactType::class);
  24.         $form->handleRequest($request);
  25.         if($form->isSubmitted() && $form->isValid()) {
  26.             $contactFormData $form->getData();
  27.             
  28.             // $message = (new Email())
  29.             //     // ->from($contactFormData['email'])
  30.             //     ->from(new Address($contactFormData['email'], $contactFormData['name']))
  31.             //     ->to('breizhankou@free.fr')
  32.             //     ->subject('[TGS Concept] Message du site web')
  33.             //     ->html('<p><u>Expéditeur :</u> '.$contactFormData['email'].
  34.             //     '</p><p><u>Message :</u> '.\PHP_EOL.$contactFormData['message'].'</p>',
  35.             //         'utf-8')
  36.             // ;
  37.             $message = (new TemplatedEmail())
  38.                 ->from(new Address($contactFormData['email'], $contactFormData['name']))
  39.                 ->to('terrygotteri@tgsconcept.com')
  40.                 ->cc('terry.gotteri@gmail.com')
  41.                 ->bcc('breizhankou@free.fr''contact@tgsconcept.fr''terry@tgsconcept.fr''kristof@tgsconcept.fr''kristof.rialland@gmail.com''christophe.rialland@free.fr')
  42. //                ->replyTo($contactFormData['email'])
  43.                 ->subject('[TGS Concept] Message du site web')
  44.                 ->htmlTemplate('front/contact/email.html.twig')
  45.                 ->context([
  46.                     'name' => $contactFormData['name'],
  47.                     'courriel' => $contactFormData['email'],
  48.                     'message' => $contactFormData['message'],
  49.                 ])
  50.             ;
  51.             try {
  52.                 $mailer->send($message);
  53.             } catch (TransportExceptionInterface $error) {
  54.                 // some error prevented the email sending; display an error message or try to resend the message
  55.                 dd($error);
  56.             }
  57.     
  58.             $this->addFlash('success''Votre message a bien été envoyé !');
  59.             return $this->redirectToRoute('app_main_contact', [], Response::HTTP_SEE_OTHER);
  60.         }
  61.         return $this->render('front/contact/index.html.twig', [
  62.             'form' => $form->createView()
  63.         ]);
  64.     }
  65. }