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:
namespace Ml\FrontendBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;
class ContactType extends AbstractType
{
public function buildForm
(FormBuilderInterface
$builder, array $options) {
$builder
->add('name', 'text', array('label' => 'Imię i nazwisko', 'pattern' => '.{2,}' // minlength
)
))
->add('email', 'email', array('label' => 'Adres e-mail') ))
->add('subject', 'text', array('label' => 'Temat', 'pattern' => '.{3,}' // minlength
)
))
->add('message', 'textarea', array('label' => 'Wiadomość', 'cols' => 40,
'rows' => 10,
)
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$collectionConstraint = new Collection
(array( new NotBlank
(array('message' => 'Name should not be blank.')), new Length
(array('min' => 2)) ),
new NotBlank
(array('message' => 'Email should not be blank.')), new Email
(array('message' => 'Invalid email address.')) ),
new NotBlank
(array('message' => 'Subject should not be blank.')), new Length
(array('min' => 3)) ),
new NotBlank
(array('message' => 'Message should not be blank.')), new Length
(array('min' => 5
)) )
));
$resolver->setDefaults(array( 'constraints' => $collectionConstraint
));
}
public function getName()
{
return 'ml_frontendbundle_contact';
}
}
Fragment z kontrolera:
use Ml\FrontendBundle\Form\ContactType;
public function contactAction(Request $request)
{
$form = $this->createForm(new ContactType());
$mailer = $this->get('mailer');
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$mailer = $this->get('mailer');
$message_content = $mailer->createMessage()
->setSubject($form->get('subject')->getData())
->setFrom($form->get('email')->getData())
->setTo('przykladowymail@mail.com')
->setBody(
$this->renderView(
'MlFrontendBundle:Default:message.html.twig',
'ip' => $this->container->get('request_stack')->getCurrentRequest()->getClientIp(),
'name' => $form->get('name')->getData(),
'email' => $form->get('email')->getData(),
'subject' => $form->get('subject')->getData(),
'message' => $form->get('message')->getData(),
)
), 'text/html'
);
$mailer->send($message_content);
$this->get('session')->getFlashBag()->add('success', "Twoja wiadomość została wysłana. Dziękujemy.");
return $this->redirect($this->generateUrl('_kontakt'));
}
}
return $this->render('MlFrontendBundle:Default:contact.html.twig', array('form' => $form->createView())); }
contact.html.twig:
<div class="contact-form-wrapper"> <h3>{% trans %}Formularz kontaktowy{% endtrans %}
</h3>
<form action="{{ path('_contact') }}" method="post"> {{ form_widget(form) }}
<button class="btn btn-primary pull-right" type="submit"><span class="glyphicon glyphicon-send"></span> {% trans %}Wyślij{% endtrans %}
</button>
message.html.twig:
IP nadawcy: {{ ip }}
Imię i nazwisko: {{ name }}
Adres e-mail: {{ email }}
Temat: {{ subject }}
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