src/JuridicusBundle/Controller/KontaktController.php line 31

Open in your IDE?
  1. <?php
  2. namespace JF\JuridicusBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Swift_Mailer;
  12. use Swift_Message;
  13. /**
  14.  * Preise controller.
  15.  *
  16.  */
  17. class KontaktController extends AbstractController
  18. {
  19.     public function __construct(
  20.         private Swift_Mailer $mailer) {
  21.     }
  22.     /**
  23.      * @Route("/contact", name="contact")
  24.      */
  25.     public function indexAction(Request $request)
  26.     {
  27.         $success false;
  28.         
  29.         $form $this->createFormBuilder()
  30.             ->add('name'TextType::class,[
  31.                     'required' => true,
  32.                     'label' => false,
  33.                     'attr' => array(
  34.                          'placeholder' => 'Mein Name'
  35.                      )
  36.             ])
  37.             ->add('email'EmailType::class,[
  38.                     'required' => true,
  39.                     'label' => false,
  40.                     'attr' => array(
  41.                          'placeholder' => 'Deine E-Mail'
  42.                      )
  43.             ])
  44.             ->add('message'TextareaType::class,[
  45.                     'required' => true,
  46.                     'label' => false,
  47.                     'attr' => array(
  48.                          'placeholder' => 'Deine Nachricht'
  49.                      )
  50.             ])
  51.             ->add('send'SubmitType::class, [
  52.                 'label' => "Einreichen",
  53.                 'attr'=> [
  54.                     'class'=>'btn btn-danger'
  55.                 ]    
  56.             ])
  57.             ->getForm();
  58.         $form->handleRequest($request);
  59.         if ($form->isSubmitted() && $form->isValid()) {
  60.             // data is an array with "name", "email", and "message" keys
  61.             $data $form->getData();
  62.             $emailBody "Neue Kontaktanfrage: ".PHP_EOL."
  63. Name: $data[name] ".PHP_EOL."
  64. Email: $data[email] ".PHP_EOL."
  65. Nachricht: $data[message] ".PHP_EOL."
  66.             ";
  67.             $mailerBcc $this->getParameter('mailer_bcc');
  68.             $mailUser     $this->getParameter('mailer_user');
  69.             $message = (new Swift_Message("Juridicus: Kontaktanfrage"))
  70.                     ->setFrom(array($mailUser => 'Juridicus'))
  71.                     ->setReplyTo('info@juridicus.de')
  72.                     ->setTo("paulgamper@yahoo.com")
  73.                     ->setBody($emailBody'text/plain')
  74.             ;
  75.             if ($this->mailer->send($message)) {
  76.                 $success true;
  77.             } 
  78.             
  79.         }
  80.         // ... render the form
  81.         
  82.         return $this->render("@JFJuridicusBundle/Kontakt/index.html.twig", array(
  83.                 'form'         => $form->createView(),
  84.                 'success'     => $success
  85.             )
  86.         );
  87.     }
  88. }