src/Controller/ContactController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactType;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Email;
  10. class ContactController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/contact", name="contact", methods={"GET","POST"})
  14.      */
  15.     public function index(Request $requestMailerInterface $mailer): Response
  16.     {
  17.         $form $this->createForm(ContactType::class);
  18.         $form->handleRequest($request);
  19.         if ($form->isSubmitted() && $form->isValid()) {
  20.             $data $form->getData();
  21.             #$to = 'contact@yourdomain.com'; // CHANGE THIS
  22.             $to 'info@ctssys.com'// CHANGE THIS
  23.             $email = (new Email())
  24.                 ->from($to)                 // keep from your domain
  25.                 ->replyTo($data['email'])   // user email goes here
  26.                 ->to($to)
  27.                 ->subject('CogniEdu Contact Form')
  28.                 ->text(
  29.                     "Name: {$data['name']}\n" .
  30.                     "Email: {$data['email']}\n\n" .
  31.                     $data['message']
  32.                 );
  33.             $mailer->send($email);
  34.             $this->addFlash('success''Thanks — your message has been sent.');
  35.             return $this->redirectToRoute('contact');
  36.         }
  37.         return $this->render('contact/index.html.twig', [
  38.             'form' => $form->createView(),
  39.         ]);
  40.     }
  41. }