<?php
namespace App\Controller;
use Symfony\Component\Mime\Email;
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;
use App\Controller\ContactController;
class MailerController extends AbstractController
{
/**
* Email sender form web site
*/
#[Route('/contact', name: 'contact_page')]
public function sendEmail(Request $request, MailerInterface $mailer): Response
{
$form = $this->createForm(ContactController::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contactFormData = $form->getData();
$email = (new Email())
->from($contactFormData['fullname'] . '<' . $contactFormData['email'] . '>')
->to($contactFormData['service'])
->subject('Vous avez reçu un message du site web Ctobi France')
// ->text('here the text from the site')
->html(
'<p>Le correspondant : <h3>' . $contactFormData['fullname'] . '</h3>
a laissé le message suivant: ' . $contactFormData['message'] . '</p>
<p> Cette action est demandé en retour: ' . $contactFormData['action'] . '</p>'
);
$mailer->send($email);
$this->addFlash('success', 'Envoi reussi !');
return $this->redirectToRoute('contact_page');
} else {
$this->addFlash('warning', 'Echec de l\'envoi !');
}
// return $this->render('pages/contact.html.twig', [
// 'formView' => $form->createView()
// ]);
return $this->renderForm('pages/contact.html.twig', [
'form' => $form,
]);
}
}