Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF2][SF] Problem z wysylka maila z formularza kontaktowego
swiezak
post
Post #1





Grupa: Zarejestrowani
Postów: 159
Pomógł: 0
Dołączył: 21.08.2011

Ostrzeżenie: (0%)
-----


Witam Forumowiczow.
Uzywam wersji 2.7 frameworka Symfony i mam problem z wysylka maila z formularza kontaktowego.
Niby obiekty nie sa puste, a mail nie wysyla sie. Byc moze cos przeoczylem w kodzie.

ContactType.php:
  1. namespace Ml\FrontendBundle\Form;
  2.  
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  6. use Symfony\Component\Validator\Constraints\Email;
  7. use Symfony\Component\Validator\Constraints\Length;
  8. use Symfony\Component\Validator\Constraints\NotBlank;
  9. use Symfony\Component\Validator\Constraints\Collection;
  10.  
  11. class ContactType extends AbstractType
  12. {
  13. public function buildForm(FormBuilderInterface $builder, array $options)
  14. {
  15. $builder
  16. ->add('name', 'text', array('label' => 'Imię i nazwisko',
  17. 'attr' => array(
  18. 'pattern' => '.{2,}' // minlength
  19. )
  20. ))
  21. ->add('email', 'email', array('label' => 'Adres e-mail')
  22. ))
  23. ->add('subject', 'text', array('label' => 'Temat',
  24. 'attr' => array(
  25. 'pattern' => '.{3,}' // minlength
  26. )
  27. ))
  28. ->add('message', 'textarea', array('label' => 'Wiadomość',
  29. 'attr' => array(
  30. 'cols' => 40,
  31. 'rows' => 10,
  32. )
  33. ));
  34. }
  35.  
  36. public function setDefaultOptions(OptionsResolverInterface $resolver)
  37. {
  38. $collectionConstraint = new Collection(array(
  39. 'name' => array(
  40. new NotBlank(array('message' => 'Name should not be blank.')),
  41. new Length(array('min' => 2))
  42. ),
  43. 'email' => array(
  44. new NotBlank(array('message' => 'Email should not be blank.')),
  45. new Email(array('message' => 'Invalid email address.'))
  46. ),
  47. 'subject' => array(
  48. new NotBlank(array('message' => 'Subject should not be blank.')),
  49. new Length(array('min' => 3))
  50. ),
  51. 'message' => array(
  52. new NotBlank(array('message' => 'Message should not be blank.')),
  53. new Length(array('min' => 5))
  54. )
  55. ));
  56.  
  57. $resolver->setDefaults(array(
  58. 'constraints' => $collectionConstraint
  59. ));
  60. }
  61.  
  62. public function getName()
  63. {
  64. return 'ml_frontendbundle_contact';
  65. }
  66. }


Fragment z kontrolera:
  1. use Ml\FrontendBundle\Form\ContactType;
  2.  
  3. public function contactAction(Request $request)
  4. {
  5. $form = $this->createForm(new ContactType());
  6. $mailer = $this->get('mailer');
  7.  
  8. if ($request->isMethod('POST')) {
  9. $form->handleRequest($request);
  10.  
  11. if ($form->isValid()) {
  12. $mailer = $this->get('mailer');
  13.  
  14. $message_content = $mailer->createMessage()
  15. ->setSubject($form->get('subject')->getData())
  16. ->setFrom($form->get('email')->getData())
  17. ->setTo('przykladowymail@mail.com')
  18. ->setBody(
  19. $this->renderView(
  20. 'MlFrontendBundle:Default:message.html.twig',
  21. 'ip' => $this->container->get('request_stack')->getCurrentRequest()->getClientIp(),
  22. 'name' => $form->get('name')->getData(),
  23. 'email' => $form->get('email')->getData(),
  24. 'subject' => $form->get('subject')->getData(),
  25. 'message' => $form->get('message')->getData(),
  26. )
  27. ), 'text/html'
  28. );
  29.  
  30. $mailer->send($message_content);
  31. $this->get('session')->getFlashBag()->add('success', "Twoja wiadomość została wysłana. Dziękujemy.");
  32.  
  33. return $this->redirect($this->generateUrl('_kontakt'));
  34. }
  35. }
  36.  
  37. return $this->render('MlFrontendBundle:Default:contact.html.twig', array('form' => $form->createView()));
  38. }


contact.html.twig:
  1. <div class="contact-form-wrapper">
  2. <h3>{% trans %}Formularz kontaktowy{% endtrans %}</h3>
  3.  
  4. <form action="{{ path('_contact') }}" method="post">
  5. {{ form_widget(form) }}
  6.  
  7. <button class="btn btn-primary pull-right" type="submit"><span class="glyphicon glyphicon-send"></span> {% trans %}Wyślij{% endtrans %}</button>
  8. </form>
  9. </div>


message.html.twig:
  1. IP nadawcy: {{ ip }}
  2. Imię i nazwisko: {{ name }}
  3. Adres e-mail: {{ email }}
  4. Temat: {{ subject }}
  5.  
  6. Wiadomość: {{ message|raw }}



Bylbym wdzieczny za pomoc przy rozwiklaniu zagadki, skad sie bierze problem z wysylka maila.

Ten post edytował swiezak 19.02.2016, 08:43:13
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 15.09.2025 - 05:04