/**
  * @param Newsletter $newsletter
  * @param array $users
  */
 public function sendNewsletter(Newsletter $newsletter, array $users)
 {
     /** @var User $user */
     foreach ($users as $user) {
         $message = \Swift_Message::newInstance()->setSubject($newsletter->getSubject())->setFrom([$this->noreplyEmailAddress => $this->noreplySenderName])->setTo($user->getEmail(), $user->getUsername())->setBody($this->templating->render('AppBundle:Emails:newsletter.html.twig', ['message' => $newsletter->getMessage()]), 'text/html');
         $this->mailer->send($message);
     }
 }
Exemplo n.º 2
0
 /**
  * @Route("/newsletter/register", name="front_newsletter_register")
  */
 public function registerNewsletterAction(Request $request)
 {
     $mail = $request->request->get('bcp-newsletter-mail');
     $emailConstraint = new EmailConstraint();
     $emailConstraint->message = 'Votre adresse mail est invalide...';
     $errors = $this->get('validator')->validate($mail, $emailConstraint);
     $em = $this->getDoctrine()->getManager();
     $newsletterAlreadyExist = $em->getRepository('AppBundle:Newsletter')->findOneBy(array('mail' => $mail));
     $userMailAlreadyExist = $em->getRepository('UserBundle:User')->findOneBy(array('email' => $mail));
     if (null !== $newsletterAlreadyExist || null !== $userMailAlreadyExist) {
         $request->getSession()->getFlashBag()->add('danger', 'Cette adresse mail a déjà été ajoutée.');
     } elseif ($errors->count() === 0 && $mail != '') {
         $newsletter = new Newsletter();
         $newsletter->setMail($mail);
         $newsletter->setToken($this->get('fos_user.util.token_generator')->generateToken());
         $em->persist($newsletter);
         $em->flush();
         $flash = sprintf('L\'adresse mail "%s" a bien été ajoutée. Vous recevrez dès à présent nos nouveautés.', $mail);
         $request->getSession()->getFlashBag()->add('success', $flash);
     } else {
         $request->getSession()->getFlashBag()->add('danger', $emailConstraint->message);
     }
     return $this->redirect($this->generateUrl('homepage'));
 }
 /**
  * @param Request $request
  * @param Newsletter $newsletter
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  * @Route("/{id}", requirements={"id": "\d+"}, name="delete_newsletter")
  * @Method("DELETE")
  */
 public function deleteAction(Request $request, Newsletter $newsletter)
 {
     $form = $this->get('app.delete_form_service')->createEntityDeleteForm($newsletter->getId(), 'delete_newsletter');
     $form->handleRequest($request);
     if ($form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $em->remove($newsletter);
         $em->flush();
     }
     return $this->redirect($this->generateUrl('admin_newsletters'));
 }