<?php
namespace App\Controller;
use App\Form\ContactType;
use App\Repository\LabelRepository;
use Symfony\Component\Mime\Address;
use App\Repository\SettingRepository;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ContactController extends AbstractController
{
/**
* @Route("/contact", name="app_contact")
*/
public function index(
SettingRepository $settingRepository,
LabelRepository $labelRepository,
Request $request,
MailerInterface $mailer
): Response
{
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
$dataMail = $settingRepository->findAll();
$siteEmail = $dataMail[0]->getSiteEmail();
$siteName = $dataMail[0]->getSiteName();
if ($form->isSubmitted() && $form->isValid()) {
$contactFormData = $form->getData();
$email = (new TemplatedEmail())
->from($contactFormData['email'])
->to(new Address($siteEmail, $siteName))
//->subject('Message depuis votre site web: ' . $contactFormData['objet'])
->subject('Message depuis votre site web')
->htmlTemplate('contact/contact_email.html.twig')
->context([
'sender' => $contactFormData['prenom'] . ' ' . $contactFormData['nom'],
//'objet' => $contactFormData['objet'],
'senderEmail' => $contactFormData['email'],
'telephone' => $contactFormData['telephone'],
//'rappel' => $contactFormData['rappel'],
'message' => trim(nl2br($contactFormData['message'])),
'settings' => $settingRepository->findAll(),
])
;
$mailer->send($email);
$this->addFlash('success', 'Merci pour votre message. Nous vous recontacterons dans les plus brefs délais.');
return $this->redirectToRoute('app_contact');
}
return $this->render('contact/index.html.twig', [
'form' => $form->createView(),
'settings' => $settingRepository->findAll(),
'labels' => $labelRepository->findAll(),
'pageTitle' => 'contact'
]);
}
}