Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF2][Symfony2][Symfony]Swiftmailer, Wysyłanie wiadomości przez formularz kontaktowy
kosmos
post
Post #1





Grupa: Zarejestrowani
Postów: 367
Pomógł: 17
Dołączył: 4.03.2008

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


Witam,
Mam problem z wysyłaniem danych z formularza na konto pocztowe przy pomocy Swiftmailera.

Formularz widok:

  1. <h2>Formularz Kontaktowy</h2>
  2. <?php echo $view['form']->start($form)?> // Nie używam tutaj przykładowo <form action = "{{path('success') }}" method="POST". Jak wskazać namiar na akcję kontrolera
  3. // po wysłaniu formularza?
  4. <?php echo $view['form']->errors($form) ?>
  5. <div>
  6. <?php echo $view['form']->row($form['name'],array(
  7. 'label' => false,
  8. 'attr' => array(
  9. 'class' => 'name',
  10. 'placeholder' => ' Imię'),)) ?>
  11. </div>
  12. <div>
  13. <?php echo $view['form']->row($form['surname'],array(
  14. 'label' => false,
  15. 'attr' => array(
  16. 'class' => 'surname',
  17. 'placeholder' => ' Nazwisko'),)) ?>
  18. </div>
  19. <div>
  20. <?php echo $view['form']->row($form['email'],array(
  21. 'label' => false,
  22. 'attr' => array(
  23. 'class' => 'email',
  24. 'placeholder' => ' Email'),)) ?>
  25. </div>
  26. <div>
  27. <?php echo $view['form']->row($form['subject'],array(
  28. 'label' => false,
  29. 'attr' => array(
  30. 'class' => 'subject',
  31. 'placeholder' => ' Temat'),)) ?>
  32. </div>
  33. <div>
  34. <?php echo $view['form']->row($form['note'],array(
  35. 'label' => false,
  36. 'attr' => array(
  37. 'class' => 'note',
  38. 'placeholder' => ' Wiadomość...'),
  39. )
  40. ) ?>
  41. </div>
  42.  
  43. <input type="submit" class="btn-form-style" value="WYŚLIJ" ?>"/>
  44.  
  45. <?php echo $view['form']->end($form) ?>


Message.php
<?php

// src/Acme/PageBundle/Entity/Message.php
namespace Acme\PageBundle\Entity;

  1. class Message
  2. {
  3. protected $name;
  4. protected $surname;
  5. protected $email;
  6. protected $subject;
  7. protected $note;
  8.  
  9.  
  10. public function getMessage()
  11. {
  12. return $this->message;
  13. }
  14. public function setMessage($message)
  15. {
  16. $this->message = $message;
  17. }
  18.  
  19. public function getName()
  20. {
  21. return $this->name;
  22. }
  23. public function setName($name)
  24. {
  25. $this->name = $name;
  26. }
  27.  
  28. public function getSurname()
  29. {
  30. return $this->surname;
  31. }
  32. public function setSurname($surname)
  33. {
  34. $this->surname = $surname;
  35. }
  36.  
  37. public function getEmail()
  38. {
  39. return $this->email;
  40. }
  41. public function setEmail($email)
  42. {
  43. $this->email = $email;
  44. }
  45.  
  46. public function getSubject()
  47. {
  48. return $this->subject;
  49. }
  50. public function setSubject($subject)
  51. {
  52. $this->subject = $subject;
  53. }
  54.  
  55. public function getNote()
  56. {
  57. return $this->note;
  58. }
  59. public function setNote($note)
  60. {
  61. $this->note = $note;
  62. }
  63. }
  64.  
  65. ?>


Controller

  1. public function contactAction(Request $request)
  2. {
  3.  
  4. $message = new Message();
  5. $form = $this->createForm(new ContactType(),$message);
  6.  
  7.  
  8.  
  9. if ($form->isValid()) { // Moge zastosować tu taki warunek?
  10. $message = \Swift_Message::newInstance()
  11. ->setSubject($message->getSubject())
  12. ->setFrom(array($message->getEmail()))
  13. ->setTo('.....@gmail.com')
  14. ->setBody($message->getNote());
  15. $this->get('mailer')->send($message);
  16. return $this->redirect($this->generateUrl('acme_task_success'));
  17. }
  18.  
  19.  
  20. return $this->render('AcmePageBundle:Default:contact.html.php', array(
  21. 'form' => $form->createView(),
  22. ));
  23.  
  24. }


Budowanie formularza

<?php
// src/Acme/PageBundle/Form/Type/ContactType.php
namespace Acme\PageBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
  1. class ContactType extends AbstractType
  2. {
  3. public function buildForm(FormBuilderInterface $builder, array $options)
  4. {
  5. $builder
  6. ->add('name','text')
  7. ->add('surname','text')
  8. ->add('email','email')
  9. ->add('subject','text')
  10. ->add('note','textarea');
  11. }
  12.  
  13. public function getName()
  14. {
  15. return 'name';
  16. }
  17. }


routing.yml
  1. acme_task_success:
  2. path: /homepage/
  3. defaults: { _controller: AcmePageBundle:Default:homepage }




Całość niestety nie działa. Po wypełnieniu pól oraz wysłaniu formularza, następuje przekierowanie na tą samą stronę, a mail nie dociera do odbiorcy.

Prośba o wsparcie w tym temacie.


Jeszcze:

  1. # Swiftmailer Configuration
  2. swiftmailer:
  3. transport: smtp
  4. host: mail.nazwadomeny.pl
  5. username: kontakt.nazwadomeny.pl
  6. password: haslo
  7. spool: { type: memory }


Na gmailu, localhoście, wszystko chodziło jak należy.

Ten warunek
  1. if ($form->isValid()) {

nie działa.

Może po kolei uda mi sie rozwiązac z Waszą pomoca ten problem.
Więc krok po kroku ...
Po wywołaniu akcji send na formularzu, jak powinienem poprawnie przekazać jego dane do kontrolera a konkretnie akcji contactAction ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kosmos
post
Post #2





Grupa: Zarejestrowani
Postów: 367
Pomógł: 17
Dołączył: 4.03.2008

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


memory - dzięki za wskazanie błędu.

Poprawiłem i zmieniłem nieco kod

Controller:

  1. public function contactAction(Request $request)
  2. {
  3. $message = new Message();
  4. $form = $this->createForm(new ContactType(),$message);
  5. $form->handleRequest($request);
  6.  
  7. if ($form->isValid()) {
  8. $to_send = \Swift_Message::newInstance()
  9.  
  10. ->setSubject($message->getSubject())
  11. ->setFrom($message->getFrom())
  12. ->setTo('nazwadomeny.pl')
  13. ->setBody($message->getNote());
  14.  
  15. $this->get('mailer')->send($to_send);
  16. return $this->redirect($this->generateUrl('acme_page_success'));
  17. }
  18.  
  19. if (!$form->isValid()) {
  20. return $this->render('AcmePageBundle:Default:contact.html.php', array(
  21. 'form' => $form->createView(),
  22. ));
  23. }
  24. }


Message.php

  1. <?php
  2.  
  3. // src/Acme/PageBundle/Entity/Message.php
  4. namespace Acme\PageBundle\Entity;
  5.  
  6. class Message
  7. {
  8. protected $name;
  9. protected $surname;
  10. protected $from;
  11. protected $subject;
  12. protected $note;
  13.  
  14.  
  15. public function getMessage()
  16. {
  17. return $this->message;
  18. }
  19. public function setMessage($message)
  20. {
  21. $this->message = $message;
  22. }
  23.  
  24. public function getName()
  25. {
  26. return $this->name;
  27. }
  28. public function setName($name)
  29. {
  30. $this->name = $name;
  31. }
  32.  
  33. public function getSurname()
  34. {
  35. return $this->surname;
  36. }
  37. public function setSurname($surname)
  38. {
  39. $this->surname = $surname;
  40. }
  41.  
  42. public function getFrom()
  43. {
  44. return $this->from;
  45. }
  46. public function setFrom($from)
  47. {
  48. $this->from = $from;
  49. }
  50.  
  51. public function getSubject()
  52. {
  53. return $this->subject;
  54. }
  55. public function setSubject($subject)
  56. {
  57. $this->subject = $subject;
  58. }
  59.  
  60. public function getNote()
  61. {
  62. return $this->note;
  63. }
  64. public function setNote($note)
  65. {
  66. $this->note = $note;
  67. }
  68. }
  69.  
  70. ?>


Teraz po wywołaniu strony z formularzem mam :
Oops! An Error Occurred
The server returned a "500 Internal Server Error".
Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.


a w logach:

[2015-04-27 19:43:07] request.INFO: Matched route "acme_page_contact" (parameters: "_controller": "Acme\PageBundle\Controller\DefaultController::contactAction", "_route": "acme_page_contact") [] [] [2015-04-27 19:43:07] request.CRITICAL: Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException: "Neither the property "email" nor one of the methods "getEmail()", "email()", "isEmail()", "hasEmail()", "__get()" exist and have public access in class "Acme\PageBundle\Entity\Message"." at /home/kosmos/domains/nazwadomeny.pl/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php line 371 {"exception":"[object] (Symfony\\Component\\PropertyAccess\\Exception\\NoSuchPropertyException(code: 0): Neither the property \"email\" nor one of the methods \"getEmail()\", \"email()\", \"isEmail()\", \"hasEmail()\", \"__get()\" exist and have public access in class \"Acme\\PageBundle\\Entity\\Message\". at /home/kosmos/domains/nazwadomeny.pl/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:371)"} []

Pytanie dlaczego?
Pozmieniałem w encji Message nazwy metod i zmienne to fakt. Nie ma już tam zmiennej email a pomimo to w logach zwracany jest komunikat że takowa jest.

UPDATE
Znalazłem błąd:
Polegał na tym że w widoku w dalszym ciągu miałem w formularzu email zamiast from (IMG:style_emoticons/default/smile.gif)

Strona z formularzem wyświetla się, po wysłaniu formularza następuje przekierowanie na stronę home, ale wiadomość nadal nie dochodzi na skrzynkę pocztową (IMG:style_emoticons/default/dry.gif)

ZAPIS Z LOGÓW

ERROR: Exception occurred while flushing email queue: Failed to authenticate on SMTP server with username "tumojanazwadomeny.pl" using 2 possible authenticators [] []

Czyli wszystko jasne ale nie do końca ;/

UPDATE

Sukces okazało się że miałem niepoprawnie skonfigurowane uwierzytelniane do skrzynki pocztowej (IMG:style_emoticons/default/wink.gif)

Mam ostatnią rzecz do zrobienia
Obecnie w mailu przesyłam

->setSubject($message->getSubject())
->setFrom($message->getFrom())
->setTo('kontakt@nazwadomeny.pl')
->setBody($message->getNote());

ale w formularzu mam jeszcze pola name i surname. Jak mogę je umieścić w setBody?

Ten post edytował kosmos 27.04.2015, 20:14:07
Go to the top of the page
+Quote Post

Posty w temacie
- kosmos   [SF2][Symfony2][Symfony]Swiftmailer   27.04.2015, 10:12:51
- - Forti   Nie wiesz jak się pracuje z formularzami. Tworzys...   27.04.2015, 10:22:18
- - kosmos   Dodałem: [PHP] pobierz, plaintext public functio...   27.04.2015, 10:58:16
- - Turson   Masz wersje php ktora nie obsluguje jeszcze tablic...   27.04.2015, 12:06:16
- - kosmos   Obecnie mam ustawioną 5.3 ale moge zmienic na 5.4 ...   27.04.2015, 12:43:57
- - Forti   A jaki dostajesz błąd? Włącz wersje deweloperską ....   27.04.2015, 12:54:27
- - kosmos   Popatrzyłem w logi produkcyjne i miałem problem z ...   27.04.2015, 13:33:58
- - Turson   Nie getSubject a getSubject()   27.04.2015, 13:46:40
- - kosmos   Tak widzę już .... dzieki, za długo nad tym siedze...   27.04.2015, 14:10:54
- - Turson   Nadpisales zmienna message przez swifta   27.04.2015, 14:12:54
- - kosmos   Możesz jaśniej ?   27.04.2015, 14:18:49
- - memory   [PHP] pobierz, plaintext $message = Swift_Messag...   27.04.2015, 14:55:26
- - kosmos   memory - dzięki za wskazanie błędu. Poprawiłem i ...   27.04.2015, 20:12:11
- - Turson   [PHP] pobierz, plaintext $body = 'Od '.$message-...   27.04.2015, 20:26:57
- - kosmos   Dzięki Turson Wrzuciłem to do from [PHP] pobierz...   27.04.2015, 20:34:27
- - Forti   Dzisiaj ostatnio raz Tobie pomogłem z symfony Cza...   27.04.2015, 21:22:22
|- - skowron-line   Cytat(Forti @ 27.04.2015, 21:22:22 ) ...   28.04.2015, 07:28:09
- - com   @up naprawdę przyszedłeś tu tylko dla tego pomógł?...   27.04.2015, 21:44:53
|- - Forti   Cytat(com @ 27.04.2015, 22:44:53 ) @u...   28.04.2015, 08:29:17
- - com   No i fajnie, że klikałeś, inni też klikają, nie ch...   28.04.2015, 08:39:13
- - Turson   Ilość "pomógł" to przedłużenie p... #po...   28.04.2015, 08:45:57
- - com   Ta bo to ma jakiekolwiek znaczenie, nie ilość się ...   28.04.2015, 08:48:22
- - Forti   Za bardzo poważnie di tego podchodzicie ja tan lu...   28.04.2015, 09:16:02
- - skowron-line   @Forti może ton twoich wypowiedzi nie odpowiada lu...   28.04.2015, 10:00:02
- - Forti   1. temat dotyczy swiftmailera. Nie sprawdził porzą...   28.04.2015, 13:15:34


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

 



RSS Aktualny czas: 5.10.2025 - 09:05