diff --git a/app/Resources/views/admin/mail/edit.html.twig b/app/Resources/views/admin/mail/edit.html.twig index 441c5bc33..f48459e08 100644 --- a/app/Resources/views/admin/mail/edit.html.twig +++ b/app/Resources/views/admin/mail/edit.html.twig @@ -22,6 +22,15 @@
+
+ +
+ + +
{{ form_widget(form.subject) }} {{ form_label(form.subject) }} diff --git a/src/AppBundle/Controller/MailController.php b/src/AppBundle/Controller/MailController.php index 1db9100b3..ba58b2ba3 100644 --- a/src/AppBundle/Controller/MailController.php +++ b/src/AppBundle/Controller/MailController.php @@ -12,6 +12,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -155,12 +156,13 @@ public function sendAction(Request $request, \Swift_Mailer $mailer) $mailform->handleRequest($request); if ($mailform->isSubmitted() && $mailform->isValid()) { $em = $this->getDoctrine()->getManager(); - //beneficiaries + + // beneficiaries $to = $mailform->get('to')->getData(); $to = json_decode($to); $beneficiaries = $em->getRepository('AppBundle:Beneficiary')->findBy(array('id' => $to)); - //end beneficiaries - //non-member + + // non-member $cci = $mailform->get('cci')->getData(); $chips = json_decode($cci); $nonMembers = array(); @@ -183,20 +185,21 @@ public function sendAction(Request $request, \Swift_Mailer $mailer) $beneficiaries[] = $fake_beneficiary; } } - //en non-member - - $nb = 0; - $errored = []; + // from $mailerService = $this->get('mailer_service'); $from_email = $mailform->get('from')->getData(); if (in_array($from_email, $mailerService->getAllowedEmails())) { $from = array($from_email => array_search($from_email, $mailerService->getAllowedEmails())); } else { - //email not listed ! + // email not listed ! $session->getFlashBag()->add('error', 'cet email n\'est pas autorisé !'); return $this->redirectToRoute('mail_edit'); } + + $from_email_in_cc = $mailform->get('from_in_cc')->getData(); + + // content & template $contentType = 'text/html'; $content = $mailform->get('message')->getData(); $parser = new Markdown; @@ -206,18 +209,23 @@ public function sendAction(Request $request, \Swift_Mailer $mailer) if ($emailTemplate) { $content = str_replace('{{template_content}}', $content, $emailTemplate->getContent()); } - $template = $this->get('twig')->createTemplate($content); + + // send mail(s) + $nb = 0; + $errored = []; foreach ($beneficiaries as $beneficiary) { + $to = [$beneficiary->getEmail() => $beneficiary->getFirstname() . ' ' . $beneficiary->getLastname()]; + if ($from_email_in_cc) { + array_push($to, $from); + } + $body = $this->get('twig')->render($template, array('beneficiary' => $beneficiary)); try { $message = (new \Swift_Message($mailform->get('subject')->getData())) ->setFrom($from) - ->setTo([$beneficiary->getEmail() => $beneficiary->getFirstname() . ' ' . $beneficiary->getLastname()]) - ->addPart( - $body, - $contentType - ); + ->setTo($to) + ->addPart($body, $contentType); $mailer->send($message); $nb++; } catch (\Swift_RfcComplianceException $exception) { @@ -242,11 +250,11 @@ private function getMailForm() { ->setAction($this->generateUrl('mail_send')) ->setMethod('POST') ->add('from', ChoiceType::class, array( - 'label' => 'Depuis', + 'label' => 'Expéditeur', 'required' => false, 'choices' => $mailerService->getAllowedEmails() )) - ->add('to', HiddenType::class, array('label' => 'Destinataires', 'required' => true)) + ->add('to', HiddenType::class, array('label' => 'Destinataire(s)', 'required' => true)) ->add('cci', HiddenType::class, array('label' => 'Non-membres', 'required' => false)) ->add('template', EntityType::class, array( 'class' => 'AppBundle:EmailTemplate', @@ -256,6 +264,11 @@ private function getMailForm() { 'required' => false, 'label' => 'Modèle' )) + ->add('from_in_cc', CheckboxType::class, array( + 'required' => false, + 'label' => 'Mettre l\'expéditeur en copie', + 'attr' => array('class' => 'filled-in') + )) ->add('subject', TextType::class, array('label' => 'Sujet', 'required' => true)) ->add('message', MarkdownEditorType::class, array('label' => 'Message', 'required' => true, 'attr' => array('class' => 'materialize-textarea'))) ->getForm();