src/Controller/ContactController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Form\ContactType;
  5. use Symfony\Component\Mime\Email;
  6. use Symfony\Component\Mime\Address;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  15. class ContactController extends AbstractController
  16. {
  17.     #[Route('/contact'name'app_contact')]
  18.     public function index(Request $requestEntityManagerInterface $emMailerInterface $mailer): Response
  19.     {
  20.         $contact = new Contact();
  21.         $form $this->createForm(ContactType::class, $contact);
  22.         $form->handleRequest($request);
  23.         
  24.         if($form->isSubmitted() && $form->isValid()){
  25.             $contact->setCreatedAt(new \DateTime());
  26.           
  27.             $em->persist($contact);
  28.             $em->flush();
  29.             $fd $form->getData();
  30.             
  31.             $notification = (new TemplatedEmail())
  32.                 ->from(new Address('notification@kalsyma-web.fr''Notification de nouveau message'))
  33.                 ->to(new Address('kalsyma.web@gmail.com''Kalsyma-web'))
  34.                 //->cc('cc@example.com')
  35.                 ->bcc(new Address('l.p.kaffin@gmail.com''notification'))
  36.                 //->replyTo('fabien@example.com')
  37.                 ->priority(Email::PRIORITY_HIGH)
  38.                 ->subject('Nouveau message')
  39.                 ->htmlTemplate('emails/notification.html.twig')
  40.                 ->context([
  41.                     'name' => $fd->getName(),
  42.                     'telephone' => $fd->getTelephone(),
  43.                     'sender' => $fd->getEmail(),
  44.                     'message' => $fd->getMessage(),
  45.                     'sendingDate' => $fd->getCreatedAt(),
  46.                 ]);
  47.                 try {
  48.                 $mailer->send($notification);
  49.                 $this->addFlash('success'"Votre message a bien été envoyé, nous vous recontacterons dès que possible");
  50.                 return $this->redirectToRoute('app_contact');
  51.                 
  52.             } catch (TransportExceptionInterface $e) {
  53.                 // some error prevented the email sending; display an
  54.                 // error message or try to resend the message
  55.                 $this->addFlash('error''erreur lors de l\'envoi du mail' $e);
  56.             }
  57.         }
  58.         
  59.         return $this->render('contact/index.html.twig', [
  60.            'form' => $form->createView(),
  61.         ]);
  62.     }
  63. }