src/Controller/ContactController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactType;
  4. use Symfony\Component\Mime\Email;
  5. use App\Repository\ExpertRepository;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. class ContactController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/contact", name="contact")
  14.      */
  15.     public function index(ExpertRepository $expertRepositoryRequest $requestMailerInterface $mailer)
  16.     {
  17.         $form $this->createForm(ContactType::class);
  18.         $form->handleRequest($request);
  19.         if($form->isSubmitted() && $form->isValid()) {
  20.             $contactFormData $form->getData();
  21.             
  22.             // $message = (new Email())
  23.             //     ->from($contactFormData['email'])
  24.             //     ->to('m.matt63@gmail.com')
  25.             //     ->subject('Email from contact form eurcaw-re')
  26.             //     ->text('Sender : '.$contactFormData['email'].\PHP_EOL.
  27.             //         $contactFormData['message'],
  28.             //         'text/plain');
  29.             // $mailer->send($message);
  30.             $headers 'From: webmaster-eurcaw@inrae.fr' "\r\n" .
  31.             'Reply-To: matthieu.reichstadt@inrae.fr' "\r\n" .
  32.             'X-Mailer: PHP/' phpversion();
  33.             $experts $expertRepository->findByIsAdmin(true);
  34.             $mail_ok=0;
  35.             foreach ($experts as $expert
  36.             {
  37.                 if (mail($expert->getEmail(),"Email from contact form eurcaw-re","Dear admin,\nYou've received an email from EURCAW website.\n\nContact: ".$contactFormData['email']."\n\nMessage:\n".$contactFormData['message'],$headers))
  38.                     $mail_ok++;
  39.             }
  40.             if ($mail_ok 0)   $this->addFlash('success''Your email has been sent.');
  41.             else                $this->addFlash('error''Error while sending the mail.');
  42.             return $this->redirectToRoute('contact');
  43.         }
  44.         return $this->render('contact/index.html.twig', [
  45.             'form' => $form->createView()
  46.         ]);
  47.     }
  48.     
  49. }