Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF2][SF] Problem z wysylka maila z formularza kontaktowego
Forum PHP.pl > Forum > PHP > Frameworki
swiezak
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.
lukaskolista
http://symfony.com/doc/current/cookbook/email/spool.html

Korzystanie z bufora maili pozwala szybciej zakończyć wykonywanie skryptu i nie czekać na serwer pocztowy, co w przypadku formularza kontaktowego jest dobrym rozwiązaniem.
Jak chcesz wysyłać "w locie", to usuń klucz "spool" z konfiguracji.
swiezak
Opcje spool w pliku config.yml mialem domyslnie wylaczona.
Natomiast, niezaleznie od tego czy jest wlaczona, czy nie, w dalszym ciagu jest problem z wysylka wiadomosci.
lukaskolista
  1. $mailer->send($message, $failures);
  2.  
  3. var_dump($failures);


Zobacz, co zdumpuje powyższy kod.
swiezak
Zwracana jest pusta tablica.
lukaskolista
Czyli wszystko działa, nie ma błedu, a mail się nie wysyła?
swiezak
Dosc komiczna sytuacja, bo tak pisalem wczesniej, obekty sa tworzone, nie sa puste, bledow nie ma, a mail nie dochodzi. Na skrzynce pocztowej nic nie ma. Co ciekawe, Swift Mailer jest w ten sam sposob skonfigurowany do wysylania powiadomien. Tutaj wysylanie jest na podobnej zasadzie, z ta roznica, ze bez wykorzystania formularza i to dziala. Po prostu brak mi pomyslu, co moze byc przyczyna, ze mail z formularza kontaktowego sie nie wysyla.
lukaskolista
I dochodzi o przekierowania z komunikatem o powodzeniu wysłania?
swiezak
Wyswietla sie komunikat o pomyslnej wysylce maila, a tak naprawde wisi on w czarnej d...
lukaskolista
Sprawdź logi z serwera pocztowego (nadawcy).
prz3kus
zobacz komunikat na serwerze SMTP co go blokuje
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.