Kontroler:
<?php namespace Blog\BackendBundle\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Blog\BackendBundle\Entity\Email; use Blog\BackendBundle\Form\EmailType; use Blog\BackendBundle\Entity\SendEmail; use Blog\BackendBundle\Form\SendEmailType; /** * SendEmail controller. * * @Route("/email") */ class SendEmailController extends Controller { /** * Send email * * Creates a new Email entity. * * @Route("/send/newemails.html", name="mail_send_to_users") * @Method("POST") * @Template("BlogBackendBundle:Email:sendEmail.html.twig") */ public function sendAction(Request $request) { $title = $_POST['blog_backendbundle_sendemail']['Title']; $massage = $_POST['blog_backendbundle_sendemail']['textMail']; $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository('BlogBackendBundle:Email')->findAll(); // getMail() -> get users emails foreach($entities AS $e) { $name = 'Radzio'; $message = \Swift_Message::newInstance() ->setSubject($title) ->setFrom('radoslawchojnacki@tlen.pl') ->setTo($e->getMail()) ->setContentType("text/html") ->setBody($this->renderView('BlogBackendBundle:Email:email.html.twig', array('massage' => $massage))) ; } } /** * Creates a form to create a Email send form. * * @param Email $entity The entity * * @return \Symfony\Component\Form\Form The form */ private function createSendForm() { 'action' => $this->generateUrl('mail_send_to_users'), 'method' => 'POST', )); return $form; } /** * Send email site * * @Route("/send/mail.html", name="send_email") * @Method("GET") * @Template() */ public function sendEmailAction() { $entity = new Email(); $form = $this->createSendForm(); 'entity' => $entity, 'form' => $form->createView(), ); } /** * After Send email site * * @Route("/aftersend/mail.html", name="aftersend_email") * @Method("GET") * @Template() */ public function aftersendEmailAction(Request $request) { $param = $request->query->get('param'); 'entity' => $param, ); } }
Klasa formularza:
<?php namespace Blog\BackendBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints\NotBlank; class SendEmailType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ { $builder ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { 'data_class' => 'Blog\BackendBundle\Entity\SendEmail' )); } { // 'email' => new Email(array('message' => 'Invalid email address')), )); $options['validation_constraint'] = $collectionConstraint; } /** * @return string */ public function getName() { return 'blog_backendbundle_sendemail'; } }
Entity:
<?php namespace Blog\BackendBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; use Gedmo\Timestampable\Traits\TimestampableEntity; use Symfony\Component\Validator\Validation; class SendEmail { /** * @var string * * @Assert\NotBlank( message = "To pole nie może byc puste" ) */ private $Title; /** * @var string * * @Assert\NotBlank( message = "To pole nie może byc puste" ) */ private $textMail; /** * Set Title * * @param string $Title * @return Title */ public function setTitle($Title) { $this->Title = $Title; return $this; } /** * Get Title * * @return string */ public function getTitle() { return $this->Title; } /** * Set textMail * * @param string $textMail * @return textMail */ public function setTextMail($textMail) { $this->textMail = $textMail; return $this; } /** * Get textMail * * @return string */ public function getTextMail() { return $this->textMail; } }
Widok:
{% extends '::layout.html.twig' %} {% block contents %} Email HTML {{ form_start(form, {'attr': {'novalidate': 'novalidate'}} ) }} <div> {{ form_label(form.Title) }} {{ form_errors(form.Title) }} {{ form_widget(form.Title) }} </div> <div> {{ form_label(form.textMail) }} {{ form_errors(form.textMail) }} {{ form_widget(form.textMail) }} </div> {{ form_end(form) }} {% endblock %}