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. $form = $this->createFormBuilder()
  29. ->add('name', TextType::class,[
  30. 'required' => true,
  31. 'label' => false,
  32. 'attr' => array(
  33. 'placeholder' => 'Mein Name'
  34. )
  35. ])
  36. ->add('email', EmailType::class,[
  37. 'required' => true,
  38. 'label' => false,
  39. 'attr' => array(
  40. 'placeholder' => 'Deine E-Mail'
  41. )
  42. ])
  43. ->add('message', TextareaType::class,[
  44. 'required' => true,
  45. 'label' => false,
  46. 'attr' => array(
  47. 'placeholder' => 'Deine Nachricht'
  48. )
  49. ])
  50. ->add('send', SubmitType::class, [
  51. 'label' => "Einreichen",
  52. 'attr'=> [
  53. 'class'=>'btn btn-danger'
  54. ]
  55. ])
  56. ->getForm();
  57. $form->handleRequest($request);
  58. if ($form->isSubmitted() && $form->isValid()) {
  59. // data is an array with "name", "email", and "message" keys
  60. $data = $form->getData();
  61. $emailBody = "Neue Kontaktanfrage: ".PHP_EOL."
  62. Name: $data[name] ".PHP_EOL."
  63. Email: $data[email] ".PHP_EOL."
  64. Nachricht: $data[message] ".PHP_EOL."
  65. ";
  66. $mailerBcc = $this->getParameter('mailer_bcc');
  67. $mailUser = $this->getParameter('mailer_user');
  68. $message = (new Swift_Message("Juridicus: Kontaktanfrage"))
  69. ->setFrom(array($mailUser => 'Juridicus'))
  70. ->setReplyTo('info@juridicus.de')
  71. ->setTo("paulgamper@yahoo.com")
  72. ->setBody($emailBody, 'text/plain')
  73. ;
  74. if ($this->mailer->send($message)) {
  75. $success = true;
  76. }
  77. }
  78. // ... render the form
  79. return $this->render("@JFJuridicusBundle/Kontakt/index.html.twig", array(
  80. 'form' => $form->createView(),
  81. 'success' => $success
  82. )
  83. );
  84. }
  85. }