src/Controller/Front/ContactController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Component\Mailer\MailerInterface;
  4. use Symfony\Component\Mime\Email;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Component\Mime\Address;
  11. class ContactController extends AbstractController
  12. {
  13.     /**
  14.      * @Route ("/contact", name="app_main_contact", methods={"GET"})
  15.      */
  16.     public function contactAction(): Response
  17.     {
  18.         return $this->render('Main/contact.html.twig');
  19.     }
  20.     /**
  21.      * @Route("/contact-ajax", name="app_main_contact_ajax", methods={"POST"})
  22.      */
  23.     public function contactajaxAction(Request $requestMailerInterface $mailer): Response
  24.     {
  25.         if ($request->getMethod() == 'POST') {
  26.             // Send headers
  27.             header('HTTP/1.1 200 OK');
  28.             header('Status: 200 OK');
  29.             header('Content-type: application/json');
  30.             $message $request->request->get('message');
  31.             $the_mail =$request->request->get('email');
  32.             $name =$request->request->get('name');
  33.             $nonValid = array();
  34.             // Check if fields are valid
  35.             if(empty($the_mail) || !filter_var($the_mailFILTER_VALIDATE_EMAIL)) $nonValid[] = "email";
  36.             if(empty($name)) $nonValid[] = "name";
  37.             if(empty($message)) $nonValid[] = "message";
  38.             // If some of them are not valid then return error
  39.             if(!empty($nonValid)) {
  40.                 return new Response (json_encode(array(
  41.                     "status" => "error",
  42.                     "type" => "ValidationError",
  43.                     "fields" => $nonValid
  44.                 )));
  45.             }
  46.             
  47.             $email = (new TemplatedEmail())
  48.                 ->from('sharjah.masjids@gmail.com')
  49.                 ->to('sharjah.masjids@gmail.com')
  50.                 ->subject('sharjah-masjids.com')
  51.                 // path of the Twig template to render
  52.                 ->htmlTemplate('mail/contact_form.html.twig')
  53.                 // pass variables (name => value) to the template
  54.                 ->context([
  55.                     'mess' => $message,
  56.                     'name' => $name,
  57.                     'the_mail' => $the_mail,
  58.                 ])
  59.             ;
  60.             try {
  61.                 $mailer->send($email);
  62.             } catch (TransportExceptionInterface $e) {
  63.                 // some error prevented the email sending; display an
  64.                 // error message or try to resend the message
  65.             }
  66.       
  67.             return new Response(json_encode(array(
  68.                 "status" => "success"
  69.             )));
  70.         }
  71.         else {
  72.             new Response (json_encode(array(
  73.                 "status" => "error",
  74.                 "type" => "SendingError"
  75.             )));
  76.         }
  77.     }
  78. }